fix(mount-helper): confine what a setuid caller can mount and unmount

weft-mount-helper is meant to be installed setuid root, but it trusted
its caller entirely: it ran veritysetup, mount and umount from the
caller's PATH, mounted a caller's image at any directory, such as over
/etc, and unmounted any mountpoint. Any local user could have gained
root through it.

It now mounts only at an empty directory
/run/user/<uid>/weft/mnt/<name> of the real user, where weft-appd puts
its mountpoints, with every component from <uid> on owned by that user,
not writable by others and opened without following symbolic links,
and mounts through the opened directory, so the path cannot be swapped
meanwhile. It opens the image and hash tree with the real user's
permissions, checks the root hash is a hex digest, mounts read-only with
nosuid, nodev and noexec through mount(2), and unmounts only an EROFS
filesystem at such a directory. veritysetup runs from the absolute path
fixed at build time (WEFT_VERITYSETUP, /usr/sbin/veritysetup by
default), with an empty environment. Devices are named
weft-<uid>-<name>, so a user reaches only their own; a device left by an
interrupted mount is closed by the next mount or unmount at that
directory, and a user may have at most 64 open, counted from the
kernel's list of device-mapper devices under a root-owned lock. Closed standard streams
are reopened on /dev/null before anything else is opened, and errors and
panics are printed as one-line messages.

Run setuid as nobody with a PATH holding a hostile veritysetup and
mount, the helper refuses /etc and another user's directory as
mountpoints, a symbolic link to /etc, a file nobody cannot read, a
malformed root hash, and unmounting /etc or a directory with no image;
a valid request reaches veritysetup at its fixed path, also with the
standard streams closed, and the hostile programs never run. Unit tests cover the mountpoint rules.
This commit is contained in:
Marco Allegretti 2026-10-09 20:56:52 +00:00
parent a1edac63b8
commit 076ed95eb1
No known key found for this signature in database
5 changed files with 488 additions and 117 deletions

1
Cargo.lock generated
View file

@ -10530,6 +10530,7 @@ name = "weft-mount-helper"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"libc",
] ]
[[package]] [[package]]

View file

@ -10,3 +10,4 @@ path = "src/main.rs"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
libc = "0.2"

View file

@ -1,29 +1,78 @@
use std::path::Path; //! Mounts a dm-verity protected EROFS package image for weft-appd, and
//! unmounts it.
//!
//! The helper is installed setuid root, and every argument and the whole
//! environment come from the user who runs it. It therefore:
//!
//! - mounts only at an empty directory `/run/user/<uid>/weft/mnt/<name>` of
//! the real user, where each component from `<uid>` on is owned by that
//! user, not writable by others and opened without following symbolic
//! links; the mount goes through the opened directory, so the path cannot
//! be swapped meanwhile;
//! - opens the image and its hash tree with the real user's permissions;
//! - mounts read-only, without set-user-ID programs or device files;
//! - unmounts only an EROFS filesystem at such a directory;
//! - runs `veritysetup` from the absolute path fixed when it was built
//! (`WEFT_VERITYSETUP`), with an empty environment, and the mount
//! system calls itself.
use anyhow::Context; use std::ffi::CString;
use std::fs::File;
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::fs::OpenOptionsExt;
use std::path::{Component, Path};
fn main() -> anyhow::Result<()> { use anyhow::{Context, bail};
let args: Vec<String> = std::env::args().collect();
match args.get(1).map(String::as_str) { const VERITYSETUP: &str = match option_env!("WEFT_VERITYSETUP") {
Some("mount") => { Some(path) => path,
let img = args.get(2).context( None => "/usr/sbin/veritysetup",
"usage: weft-mount-helper mount <img> <hash_dev> <root_hash> <mountpoint>", };
)?; /// The parent of every user's runtime directory.
let hash_dev = args.get(3).context("missing <hash_dev>")?; const RUN_USER: &str = "/run/user";
let root_hash = args.get(4).context("missing <root_hash>")?; const EROFS_SUPER_MAGIC: libc::c_long = 0xE0F5_E1E2;
let mountpoint = args.get(5).context("missing <mountpoint>")?; /// How many package images one user may have open at a time, so a user
cmd_mount( /// cannot use up the system's device-mapper and loop devices.
Path::new(img), const MAX_DEVICES_PER_USER: usize = 64;
Path::new(hash_dev),
root_hash, fn main() {
Path::new(mountpoint), // Panics print one line too, whatever RUST_BACKTRACE says.
)?; std::panic::set_hook(Box::new(|info| {
eprintln!("weft-mount-helper: internal error: {info}");
}));
// A caller can start the helper with standard input, output or error
// closed; a file opened later would then take that number and be
// misdirected. They are opened on /dev/null first.
for fd in 0..=2 {
if unsafe { libc::fcntl(fd, libc::F_GETFD) } < 0
&& unsafe { libc::open(c"/dev/null".as_ptr(), libc::O_RDWR) } != fd
{
std::process::abort();
} }
Some("umount") => { }
let mountpoint = args // Errors are printed as messages only: a caller's RUST_BACKTRACE has no
.get(2) // effect.
.context("usage: weft-mount-helper umount <mountpoint>")?; if let Err(e) = run() {
cmd_umount(Path::new(mountpoint))?; eprintln!("weft-mount-helper: {e:#}");
std::process::exit(1);
}
}
fn run() -> anyhow::Result<()> {
let args: Vec<String> = std::env::args().collect();
match (args.get(1).map(String::as_str), args.len()) {
(Some("mount"), 6) => {
require_root()?;
cmd_mount(
Path::new(&args[2]),
Path::new(&args[3]),
&args[4],
Path::new(&args[5]),
)
}
(Some("umount"), 3) => {
require_root()?;
cmd_umount(Path::new(&args[2]))
} }
_ => { _ => {
eprintln!("usage:"); eprintln!("usage:");
@ -32,43 +81,228 @@ fn main() -> anyhow::Result<()> {
std::process::exit(1); std::process::exit(1);
} }
} }
Ok(())
}
fn effective_uid() -> Option<u32> {
let status = std::fs::read_to_string("/proc/self/status").ok()?;
status
.lines()
.find(|l| l.starts_with("Uid:"))
.and_then(|l| l.split_whitespace().nth(2))
.and_then(|s| s.parse().ok())
} }
fn require_root() -> anyhow::Result<()> { fn require_root() -> anyhow::Result<()> {
match effective_uid() { let euid = unsafe { libc::geteuid() };
Some(0) => Ok(()), if euid != 0 {
Some(uid) => anyhow::bail!("weft-mount-helper must run as root (euid={uid})"), bail!("weft-mount-helper must run as root (euid={euid})");
None => { }
anyhow::bail!("weft-mount-helper must run as root (could not read /proc/self/status)") Ok(())
} }
fn real_ids() -> (libc::uid_t, libc::gid_t) {
unsafe { (libc::getuid(), libc::getgid()) }
}
/// A mountpoint that passed the checks, held open.
struct Mountpoint {
dir: OwnedFd,
uid: libc::uid_t,
name: String,
}
impl Mountpoint {
/// The directory as a path the kernel resolves to the open directory.
fn proc_path(&self) -> String {
format!("/proc/self/fd/{}", self.dir.as_raw_fd())
}
/// The device-mapper name, from the user and the mountpoint's name,
/// which weft-appd chooses at random, so concurrent sessions get
/// distinct devices and no user can name another user's.
fn device_name(&self) -> String {
format!("{}{}", device_prefix(self.uid), self.name)
} }
} }
/// The device-mapper name for an image mounted at `mountpoint`: `weft-` fn device_prefix(uid: libc::uid_t) -> String {
/// followed by the mountpoint's last component. weft-appd names each format!("weft-{uid}-")
/// mountpoint with a random token, so concurrent sessions get distinct }
/// devices. Device-mapper names are limited to 127 bytes.
fn device_name(mountpoint: &Path) -> String { /// The names of the device-mapper devices, from the kernel's own list.
let leaf = mountpoint fn device_names() -> anyhow::Result<Vec<String>> {
.file_name() let mut names = Vec::new();
.map(|name| name.to_string_lossy()) for entry in std::fs::read_dir("/sys/block").context("cannot list block devices")? {
.unwrap_or_default(); let path = entry
let sanitized: String = leaf .context("cannot list block devices")?
.chars() .path()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' }) .join("dm/name");
.take(120) match std::fs::read_to_string(&path) {
.collect(); Ok(name) => names.push(name.trim_end().to_owned()),
format!("weft-{sanitized}") Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => return Err(e).with_context(|| format!("cannot read {}", path.display())),
}
}
Ok(names)
}
/// Serializes device creation, so the per-user limit holds against
/// concurrent mounts. The lock lives in a directory only root can write.
fn lock_devices() -> anyhow::Result<File> {
let lock = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.mode(0o600)
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
.open("/run/weft-mount-helper.lock")
.context("cannot open the device lock")?;
if unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX) } != 0 {
bail!(
"cannot lock the devices: {}",
std::io::Error::last_os_error()
);
}
Ok(lock)
}
/// A mountpoint name: what weft-appd's random tokens are made of.
fn is_mountpoint_name(name: &str) -> bool {
(1..=64).contains(&name.len())
&& name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}
/// A dm-verity root hash in hex, as `veritysetup format` prints it.
fn is_root_hash(text: &str) -> bool {
(64..=128).contains(&text.len())
&& text.len().is_multiple_of(2)
&& text.bytes().all(|b| b.is_ascii_hexdigit())
}
fn openat_dir(parent: Option<&OwnedFd>, name: &Path) -> std::io::Result<OwnedFd> {
let name = CString::new(name.as_os_str().as_encoded_bytes())
.map_err(|_| std::io::Error::from(std::io::ErrorKind::InvalidInput))?;
let dirfd = parent.map_or(libc::AT_FDCWD, |p| p.as_raw_fd());
let fd = unsafe {
libc::openat(
dirfd,
name.as_ptr(),
libc::O_PATH | libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_CLOEXEC,
)
};
if fd < 0 {
return Err(std::io::Error::last_os_error());
}
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
}
fn fstat(fd: &OwnedFd) -> std::io::Result<libc::stat> {
let mut st: libc::stat = unsafe { std::mem::zeroed() };
if unsafe { libc::fstat(fd.as_raw_fd(), &mut st) } != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(st)
}
/// Opens `mountpoint`, which must be `<run_user>/<uid>/weft/mnt/<name>`.
/// Every directory from `<uid>` on must belong to `uid` and not be writable
/// by others; for a mount, the mountpoint itself too, and it must be empty.
fn open_mountpoint(
run_user: &Path,
uid: libc::uid_t,
mountpoint: &Path,
for_mount: bool,
) -> anyhow::Result<Mountpoint> {
let base = run_user.join(uid.to_string()).join("weft/mnt");
let name = mountpoint
.strip_prefix(&base)
.ok()
.filter(|rest| {
let mut components = rest.components();
matches!(components.next(), Some(Component::Normal(_))) && components.next().is_none()
})
.and_then(|rest| rest.to_str())
.filter(|name| is_mountpoint_name(name))
.with_context(|| {
format!(
"{} is not a mountpoint in {}",
mountpoint.display(),
base.display()
)
})?
.to_owned();
let mut dir = openat_dir(None, run_user)
.with_context(|| format!("cannot open {}", run_user.display()))?;
let uid_dir = uid.to_string();
let components = [uid_dir.as_str(), "weft", "mnt", name.as_str()];
for (i, component) in components.iter().enumerate() {
dir = openat_dir(Some(&dir), Path::new(component))
.with_context(|| format!("cannot open {component} in the path to the mountpoint"))?;
// For an unmount the last one is the mounted filesystem's root.
if i == components.len() - 1 && !for_mount {
break;
}
let st = fstat(&dir)?;
if st.st_uid != uid || st.st_mode & 0o022 != 0 {
bail!("{component} in the path to the mountpoint is not the user's own directory");
}
}
let mountpoint = Mountpoint { dir, uid, name };
if for_mount
&& std::fs::read_dir(mountpoint.proc_path())
.context("cannot read the mountpoint")?
.next()
.is_some()
{
bail!("the mountpoint is not empty");
}
Ok(mountpoint)
}
/// Opens `path` with the real user's permissions, as a regular file.
fn open_as_user(path: &Path, uid: libc::uid_t, gid: libc::gid_t) -> anyhow::Result<File> {
let euid = unsafe { libc::geteuid() };
let egid = unsafe { libc::getegid() };
if unsafe { libc::setegid(gid) } != 0 || unsafe { libc::seteuid(uid) } != 0 {
let error = std::io::Error::last_os_error();
restore_ids(euid, egid);
bail!("cannot take the user's permissions: {error}");
}
let opened = std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_NOFOLLOW)
.open(path);
restore_ids(euid, egid);
let file = opened.with_context(|| format!("cannot open {}", path.display()))?;
if !file.metadata()?.file_type().is_file() {
bail!("{} is not a regular file", path.display());
}
Ok(file)
}
/// Returns to the helper's own effective IDs; continuing without them is
/// not safe, so failing aborts.
fn restore_ids(euid: libc::uid_t, egid: libc::gid_t) {
if unsafe { libc::seteuid(euid) } != 0 || unsafe { libc::setegid(egid) } != 0 {
std::process::abort();
}
}
/// Lets a child process inherit `file`.
fn inheritable(file: &File) -> anyhow::Result<String> {
let fd = file.as_raw_fd();
let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC) } < 0 {
bail!("cannot pass a file on: {}", std::io::Error::last_os_error());
}
Ok(format!("/proc/self/fd/{fd}"))
}
fn veritysetup(args: &[&str]) -> anyhow::Result<()> {
let status = std::process::Command::new(VERITYSETUP)
.env_clear()
.args(args)
.stdin(std::process::Stdio::null())
.status()
.with_context(|| format!("cannot run {VERITYSETUP}"))?;
if !status.success() {
bail!("veritysetup {} failed with {status}", args[0]);
}
Ok(())
} }
fn cmd_mount( fn cmd_mount(
@ -77,66 +311,87 @@ fn cmd_mount(
root_hash: &str, root_hash: &str,
mountpoint: &Path, mountpoint: &Path,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
require_root()?; if !is_root_hash(root_hash) {
let dev_name = device_name(mountpoint); bail!("the root hash is not a hex digest");
}
let status = std::process::Command::new("veritysetup") let (uid, gid) = real_ids();
.args([ let target = open_mountpoint(Path::new(RUN_USER), uid, mountpoint, true)?;
"open", let image = open_as_user(img, uid, gid)?;
&img.to_string_lossy(), let hash_tree = open_as_user(hash_dev, uid, gid)?;
&dev_name, let dev_name = target.device_name();
&hash_dev.to_string_lossy(), let _lock = lock_devices()?;
root_hash, let names = device_names()?;
]) // A device left by a mount interrupted before it was mounted here; the
.status() // empty mountpoint shows it is not in use.
.context("spawn veritysetup; ensure cryptsetup-bin is installed")?; if names.contains(&dev_name) {
if !status.success() { veritysetup(&["close", &dev_name])?;
anyhow::bail!("veritysetup open failed with status {status}"); }
let prefix = device_prefix(uid);
let open = names
.iter()
.filter(|name| name.starts_with(&prefix) && **name != dev_name)
.count();
if open >= MAX_DEVICES_PER_USER {
bail!("too many package images are open ({MAX_DEVICES_PER_USER})");
} }
let mapper_dev = format!("/dev/mapper/{dev_name}"); veritysetup(&[
let status = std::process::Command::new("mount") "open",
.args([ &inheritable(&image)?,
"-t", &dev_name,
"erofs", &inheritable(&hash_tree)?,
"-o", root_hash,
"ro", ])?;
&mapper_dev,
&mountpoint.to_string_lossy(),
])
.status()
.context("spawn mount")?;
if !status.success() {
let _ = std::process::Command::new("veritysetup")
.args(["close", &dev_name])
.status();
anyhow::bail!("mount failed with status {status}");
}
let source = CString::new(format!("/dev/mapper/{dev_name}"))?;
let path = CString::new(target.proc_path())?;
let fstype = CString::new("erofs")?;
// Packages hold components and UI files, which are read, never run.
let flags = libc::MS_RDONLY | libc::MS_NOSUID | libc::MS_NODEV | libc::MS_NOEXEC;
let mounted = unsafe {
libc::mount(
source.as_ptr(),
path.as_ptr(),
fstype.as_ptr(),
flags,
std::ptr::null(),
)
};
if mounted != 0 {
let error = std::io::Error::last_os_error();
let _ = veritysetup(&["close", &dev_name]);
bail!("mount failed: {error}");
}
eprintln!("mounted: {} -> {}", img.display(), mountpoint.display()); eprintln!("mounted: {} -> {}", img.display(), mountpoint.display());
Ok(()) Ok(())
} }
fn cmd_umount(mountpoint: &Path) -> anyhow::Result<()> { fn cmd_umount(mountpoint: &Path) -> anyhow::Result<()> {
require_root()?; let (uid, _) = real_ids();
let dev_name = device_name(mountpoint); let target = open_mountpoint(Path::new(RUN_USER), uid, mountpoint, false)?;
let mut fs: libc::statfs = unsafe { std::mem::zeroed() };
let status = std::process::Command::new("umount") if unsafe { libc::fstatfs(target.dir.as_raw_fd(), &mut fs) } != 0 {
.arg(mountpoint) bail!(
.status() "cannot inspect the mountpoint: {}",
.context("spawn umount")?; std::io::Error::last_os_error()
if !status.success() { );
anyhow::bail!("umount failed with status {status}");
} }
if fs.f_type as libc::c_long != EROFS_SUPER_MAGIC {
let status = std::process::Command::new("veritysetup") // A mount interrupted after its device opened leaves the device and
.args(["close", &dev_name]) // an empty mountpoint; the device is the caller's own.
.status() let dev_name = target.device_name();
.context("spawn veritysetup close; ensure cryptsetup-bin is installed")?; if device_names()?.contains(&dev_name) {
if !status.success() { veritysetup(&["close", &dev_name])?;
anyhow::bail!("veritysetup close failed with status {status}"); eprintln!("closed the unmounted device for {}", mountpoint.display());
return Ok(());
}
bail!("no package image is mounted at {}", mountpoint.display());
} }
let path = CString::new(target.proc_path())?;
if unsafe { libc::umount2(path.as_ptr(), 0) } != 0 {
bail!("umount failed: {}", std::io::Error::last_os_error());
}
veritysetup(&["close", &target.device_name()])?;
eprintln!("unmounted: {}", mountpoint.display()); eprintln!("unmounted: {}", mountpoint.display());
Ok(()) Ok(())
} }
@ -144,22 +399,136 @@ fn cmd_umount(mountpoint: &Path) -> anyhow::Result<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::os::unix::fs::PermissionsExt;
struct Layout {
root: std::path::PathBuf,
uid: libc::uid_t,
}
impl Layout {
fn new(tag: &str) -> Self {
let root = std::env::temp_dir()
.join(format!("weft_mount_helper_{tag}_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let uid = unsafe { libc::getuid() };
let mnt = root.join(uid.to_string()).join("weft/mnt");
std::fs::create_dir_all(&mnt).unwrap();
for dir in [
root.join(uid.to_string()),
root.join(format!("{uid}/weft")),
mnt,
] {
std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700)).unwrap();
}
Layout { root, uid }
}
fn mnt(&self) -> std::path::PathBuf {
self.root.join(self.uid.to_string()).join("weft/mnt")
}
}
impl Drop for Layout {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.root);
}
}
#[test] #[test]
fn device_names_follow_the_mountpoint_name() { fn an_empty_directory_of_the_user_is_a_mountpoint() {
let name = device_name(Path::new("/run/user/1000/weft/mnt/0123abcd")); let layout = Layout::new("ok");
assert_eq!(name, "weft-0123abcd"); let point = layout.mnt().join("0123abcd");
assert_ne!( std::fs::create_dir(&point).unwrap();
device_name(Path::new("/run/user/1000/weft/mnt/aaaa")), let target = open_mountpoint(&layout.root, layout.uid, &point, true).unwrap();
device_name(Path::new("/run/user/1000/weft/mnt/bbbb")) assert_eq!(
target.device_name(),
format!("weft-{}-0123abcd", layout.uid)
);
// The open directory is the mountpoint.
assert_eq!(
std::fs::canonicalize(target.proc_path()).unwrap(),
std::fs::canonicalize(&point).unwrap()
); );
} }
#[test] #[test]
fn device_names_are_sanitized_and_bounded() { fn mountpoints_elsewhere_are_refused() {
let long = format!("/mnt/{}", "x.y".repeat(100)); let layout = Layout::new("elsewhere");
let name = device_name(Path::new(&long)); let mnt = layout.mnt();
assert!(name.len() <= 127); std::fs::create_dir(mnt.join("ok")).unwrap();
assert!(name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')); for path in [
std::path::PathBuf::from("/etc"),
layout.root.join("other"),
mnt.clone(),
mnt.join("ok/deeper"),
mnt.join("../mnt/ok"),
mnt.join("bad.name"),
mnt.join("x".repeat(65)),
// Another user's directory.
layout
.root
.join((layout.uid + 1).to_string())
.join("weft/mnt/ok"),
] {
assert!(
open_mountpoint(&layout.root, layout.uid, &path, true).is_err(),
"{}",
path.display()
);
}
}
#[test]
fn symbolic_links_and_shared_directories_are_refused() {
let layout = Layout::new("links");
let mnt = layout.mnt();
// The mountpoint is a link to a directory elsewhere.
let elsewhere = layout.root.join("elsewhere");
std::fs::create_dir(&elsewhere).unwrap();
std::os::unix::fs::symlink(&elsewhere, mnt.join("link")).unwrap();
assert!(open_mountpoint(&layout.root, layout.uid, &mnt.join("link"), true).is_err());
assert!(open_mountpoint(&layout.root, layout.uid, &mnt.join("link"), false).is_err());
// A directory others may write to.
std::fs::create_dir(mnt.join("shared")).unwrap();
std::fs::set_permissions(mnt.join("shared"), std::fs::Permissions::from_mode(0o777))
.unwrap();
assert!(open_mountpoint(&layout.root, layout.uid, &mnt.join("shared"), true).is_err());
// A link in the path above the mountpoint.
let weft = layout.root.join(format!("{}/weft", layout.uid));
std::fs::rename(&weft, layout.root.join("moved")).unwrap();
std::os::unix::fs::symlink(layout.root.join("moved"), &weft).unwrap();
std::fs::create_dir(layout.root.join("moved/mnt/ok")).unwrap();
assert!(open_mountpoint(&layout.root, layout.uid, &mnt.join("ok"), true).is_err());
}
#[test]
fn a_mountpoint_must_be_empty_and_the_users_own() {
let layout = Layout::new("full");
let point = layout.mnt().join("full");
std::fs::create_dir(&point).unwrap();
std::fs::write(point.join("file"), b"x").unwrap();
assert!(open_mountpoint(&layout.root, layout.uid, &point, true).is_err());
// Unmounting looks at the mounted filesystem, not its contents.
assert!(open_mountpoint(&layout.root, layout.uid, &point, false).is_ok());
// The same layout seen as another user's.
let other = layout.uid + 1;
let theirs = layout.root.join(other.to_string());
std::fs::rename(layout.root.join(layout.uid.to_string()), &theirs).unwrap();
std::fs::remove_file(theirs.join("weft/mnt/full/file")).unwrap();
assert!(open_mountpoint(&layout.root, other, &theirs.join("weft/mnt/full"), true).is_err());
std::fs::rename(&theirs, layout.root.join(layout.uid.to_string())).unwrap();
}
#[test]
fn root_hashes_are_hex_digests() {
assert!(is_root_hash(&"a".repeat(64)));
assert!(is_root_hash(&"0F".repeat(64)));
for bad in ["", "-a", &"g".repeat(64), &"a".repeat(65), &"a".repeat(130)] {
assert!(!is_root_hash(bad), "{bad:?}");
}
} }
} }

View file

@ -36,7 +36,7 @@ Per-session file proxy. Runs as a separate process with a path allowlist derived
### weft-mount-helper ### weft-mount-helper
Setuid helper binary. Calls `veritysetup` and mounts EROFS images for dm-verity-protected packages. Its device-mapper device is named after the mountpoint, which weft-appd names with a random token in `$XDG_RUNTIME_DIR/weft/mnt`. The helper does not yet restrict its callers or the images and mountpoints they pass, so it is not part of a supported installation. Setuid-root helper that mounts the EROFS images of dm-verity protected packages for weft-appd and unmounts them. Since its caller controls every argument and the environment, it mounts only at an empty directory `/run/user/<uid>/weft/mnt/<name>` of the real user (weft-appd names it with a random token in `$XDG_RUNTIME_DIR/weft/mnt`, so `XDG_RUNTIME_DIR` must be `/run/user/<uid>`), with every component from `<uid>` on owned by that user, not writable by others and opened without following symbolic links, and mounts through the opened directory. It opens the image and hash tree with the real user's permissions, mounts read-only with `nosuid`, `nodev` and `noexec`, unmounts only an EROFS filesystem at such a directory, and runs `veritysetup` from the absolute path fixed at build time (`WEFT_VERITYSETUP`, `/usr/sbin/veritysetup` by default) with an empty environment. Its device-mapper device is `weft-<uid>-<name>`, so a user can only reach their own devices; a device left by an interrupted mount is closed by the next mount or unmount at that directory, and a user may have at most 64 open.
## Process Topology ## Process Topology

View file

@ -22,7 +22,7 @@ Apps access the filesystem only through WASI preopened directories. Each capabil
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 (`ed25519-dalek`). The signature covers the SHA-256 hash of `wapp.toml` and `app.wasm`. `weft-pack verify` checks the signature before installation.
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`. For verified read-only package storage, `weft-pack build-image` produces an EROFS image protected with dm-verity. Mounting requires the setuid-root `weft-mount-helper`, which calls `veritysetup`. Its callers can mount only images they can read, at their own runtime directory, read-only, without set-user-ID programs, devices or executables, at most 64 at a time (see `architecture.md`). A user can therefore have the kernel parse an EROFS image of their own making: the EROFS driver's handling of crafted images is part of the attack surface wherever the helper is installed. The NixOS image installs it through a `security.wrappers` entry that only the `users` group may run.
## Seccomp ## Seccomp