Merge fix/confine-file-portal: confine file portal operations beneath the granted directory (#28)

This commit is contained in:
Marco Allegretti 2026-10-10 09:25:46 +02:00 committed by GitHub
commit 656c3df8fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 600 additions and 264 deletions

1
Cargo.lock generated
View file

@ -10513,6 +10513,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"base64 0.22.1",
"rustix 1.1.4",
"serde",
"serde_json",
]

View file

@ -16,7 +16,7 @@ WEFT OS is a Wayland compositor and application runtime where every app is a Web
**Package management** — `weft-pack` handles check, sign, verify, install and update, rollback, uninstall, list, build-image (EROFS dm-verity), and info. Validates capability strings at check time. Installs only packages signed by a trusted key (or explicitly as development content) and binds each app ID to its first publisher. App data is stored apart from packages and survives uninstall.
**File portal** — `weft-file-portal` is a per-session file proxy with a path allowlist and `..` blocking.
**File portal** — `weft-file-portal` is a per-session file proxy for the session's directory grants, with every operation confined beneath the granted directory by the kernel.
**Mount helper** — `weft-mount-helper` is a setuid helper for EROFS dm-verity mount/umount via `veritysetup`.

View file

@ -346,10 +346,6 @@ pub(crate) async fn supervise(
cmd.arg("--ipc-socket").arg(&ipc_socket_path);
cmd.arg("--module").arg(&package.module);
if let Some((ref sock, _)) = portal {
cmd.env("WEFT_FILE_PORTAL_SOCKET", sock);
}
for dir in &grants.dirs {
cmd.arg("--preopen").arg(dir.preopen_arg());
}

View file

@ -13,3 +13,6 @@ anyhow = "1.0"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
base64 = "0.22"
[target.'cfg(target_os = "linux")'.dependencies]
rustix = { version = "1", features = ["fs"] }

View file

@ -1,17 +1,37 @@
#[cfg(unix)]
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
//! A per-session file proxy for the directories a session is granted.
//!
//! Requests name absolute host paths inside a granted directory. Each
//! granted directory is opened once at startup, and every operation resolves
//! the rest of the path relative to that descriptor with `openat2` and
//! `RESOLVE_BENEATH`, so the kernel keeps it inside the directory while it
//! runs: `..`, absolute symbolic links, links that point outside and links
//! replaced during the operation cannot leave it. Paths must name their
//! location directly, without `.` or `..` components. Writes replace a file
//! atomically and never create directories.
#[cfg(target_os = "linux")]
use std::io::{BufRead, BufReader, Read, Write};
use std::path::{Component, Path, PathBuf};
use serde::{Deserialize, Serialize};
#[cfg(unix)]
use std::sync::Arc;
#[cfg(unix)]
use anyhow::Context;
#[cfg(unix)]
use std::os::unix::net::{UnixListener, UnixStream};
/// The largest file read or written, in bytes.
const MAX_FILE: usize = 16 * 1024 * 1024;
/// The longest request line: a write of the largest file, base64-encoded,
/// with room for its path.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const MAX_LINE: usize = MAX_FILE / 3 * 4 + 64 * 1024;
/// The most entries one listing returns.
const MAX_ENTRIES: usize = 10_000;
/// The most connections served at once.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const MAX_CONNECTIONS: usize = 4;
/// How long a connection may stay silent before it is closed.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
/// The prefix of the temporary files writes create, never listed.
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
const TEMPORARY_PREFIX: &str = ".weft-portal-";
#[derive(Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
@ -38,8 +58,13 @@ impl Response {
}
}
#[cfg(unix)]
#[cfg(target_os = "linux")]
fn main() -> anyhow::Result<()> {
use anyhow::Context;
use std::os::unix::net::UnixListener;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
eprintln!(
@ -49,32 +74,59 @@ fn main() -> anyhow::Result<()> {
}
let socket_path = &args[1];
let allowed = Arc::new(parse_allowed(&args[2..]));
let mut roots = Vec::new();
for grant in parse_allowed(&args[2..]) {
match Root::open(grant) {
Ok(root) => roots.push(root),
Err((path, e)) => eprintln!("cannot open granted directory {}: {e}", path.display()),
}
}
let roots = Arc::new(roots);
if Path::new(socket_path).exists() {
std::fs::remove_file(socket_path)
.with_context(|| format!("remove stale socket {socket_path}"))?;
}
let listener =
UnixListener::bind(socket_path).with_context(|| format!("bind {socket_path}"))?;
let active = Arc::new(AtomicUsize::new(0));
for stream in listener.incoming() {
match stream {
Ok(s) => {
let allowed = Arc::clone(&allowed);
std::thread::spawn(move || handle_connection(s, &allowed));
let mut stream = match stream {
Ok(s) => s,
Err(e) => {
eprintln!("accept error: {e}");
continue;
}
Err(e) => eprintln!("accept error: {e}"),
};
if active.fetch_add(1, Ordering::SeqCst) >= MAX_CONNECTIONS {
active.fetch_sub(1, Ordering::SeqCst);
let _ = stream.write_all(b"{\"error\":\"too many connections\"}\n");
continue;
}
// The slot is released however the connection ends, a panic
// included.
struct Slot(Arc<AtomicUsize>);
impl Drop for Slot {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::SeqCst);
}
}
let slot = Slot(Arc::clone(&active));
let roots = Arc::clone(&roots);
if let Err(e) = std::thread::Builder::new().spawn(move || {
let _slot = slot;
handle_connection(stream, &roots);
}) {
eprintln!("cannot serve a connection: {e}");
}
}
Ok(())
}
#[cfg(not(unix))]
#[cfg(not(target_os = "linux"))]
fn main() -> anyhow::Result<()> {
anyhow::bail!("weft-file-portal requires a Unix platform")
anyhow::bail!("weft-file-portal requires Linux")
}
/// A directory the session may use: `--allow` grants reading and writing,
@ -85,7 +137,6 @@ struct Allowed {
writable: bool,
}
#[cfg_attr(not(any(unix, test)), allow(dead_code))]
fn parse_allowed(args: &[String]) -> Vec<Allowed> {
let mut allowed = Vec::new();
let mut i = 0;
@ -109,33 +160,105 @@ fn parse_allowed(args: &[String]) -> Vec<Allowed> {
allowed
}
#[cfg_attr(not(any(unix, test)), allow(dead_code))]
fn normalize_path(path: &Path) -> PathBuf {
use std::path::Component;
let mut out = PathBuf::new();
for c in path.components() {
match c {
Component::ParentDir => {
out.pop();
/// The part of `path` inside the granted directory `root`, if `path` lies
/// in it and names its location directly: an absolute path of plain
/// components only, without `.`, `..`, empty components or a trailing `/`.
/// The text is checked as given, since path parsing drops `.` and repeated
/// separators.
fn relative_to<'a>(path: &'a Path, root: &Path) -> Option<&'a Path> {
let text = path.to_str()?;
let direct = text.starts_with('/')
&& text[1..]
.split('/')
.all(|segment| !matches!(segment, "" | "." | ".."));
if !direct {
return None;
}
Component::CurDir => {}
other => out.push(other),
}
}
out
let rest = path.strip_prefix(root).ok()?;
rest.components()
.all(|c| matches!(c, Component::Normal(_)))
.then_some(rest)
}
/// Whether `path` lies in an allowed directory, and for a write, in one
/// granted read-write.
fn is_allowed(path: &Path, allowed: &[Allowed], write: bool) -> bool {
let norm = normalize_path(path);
allowed
/// A granted directory, held open for the life of the portal.
#[cfg(target_os = "linux")]
struct Root {
path: PathBuf,
dir: rustix::fd::OwnedFd,
writable: bool,
}
#[cfg(target_os = "linux")]
impl Root {
fn open(grant: Allowed) -> Result<Self, (PathBuf, std::io::Error)> {
use rustix::fs::{Mode, OFlags};
match rustix::fs::open(
&grant.root,
OFlags::RDONLY | OFlags::DIRECTORY | OFlags::CLOEXEC,
Mode::empty(),
) {
Ok(dir) => Ok(Self {
path: grant.root,
dir,
writable: grant.writable,
}),
Err(e) => Err((grant.root, e.into())),
}
}
}
/// The granted directory holding `path` and the path inside it, for an
/// operation that writes when `write` is set.
#[cfg(target_os = "linux")]
fn locate<'a, 'p>(
roots: &'a [Root],
path: &'p str,
write: bool,
) -> Result<(&'a Root, &'p Path), Response> {
let requested = Path::new(path);
roots
.iter()
.any(|a| norm.starts_with(&a.root) && (a.writable || !write))
.filter(|root| root.writable || !write)
.find_map(|root| relative_to(requested, &root.path).map(|rest| (root, rest)))
.ok_or_else(|| Response::err(format!("access denied: {path}")))
}
#[cfg(unix)]
fn handle_connection(stream: UnixStream, allowed: &[Allowed]) {
/// Opens `rest` inside `root`, never leaving it.
#[cfg(target_os = "linux")]
fn open_beneath(
root: &Root,
rest: &Path,
flags: rustix::fs::OFlags,
) -> std::io::Result<rustix::fd::OwnedFd> {
use rustix::fs::{Mode, OFlags, ResolveFlags};
let rest = if rest.as_os_str().is_empty() {
Path::new(".")
} else {
rest
};
rustix::fs::openat2(
&root.dir,
rest,
flags | OFlags::CLOEXEC | OFlags::NOCTTY,
Mode::empty(),
ResolveFlags::BENEATH | ResolveFlags::NO_MAGICLINKS,
)
.map_err(|e| {
// The kernel reports a resolution that would leave the directory as
// EXDEV.
if e == rustix::io::Errno::XDEV {
std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"access denied: the path leads out of the granted directory",
)
} else {
e.into()
}
})
}
#[cfg(target_os = "linux")]
fn handle_connection(stream: std::os::unix::net::UnixStream, roots: &[Root]) {
let mut writer = match stream.try_clone() {
Ok(s) => s,
Err(e) => {
@ -143,22 +266,33 @@ fn handle_connection(stream: UnixStream, allowed: &[Allowed]) {
return;
}
};
let reader = BufReader::new(stream);
for line in reader.lines() {
let line = match line {
Ok(l) => l,
Err(_) => break,
};
if line.is_empty() {
let _ = stream.set_read_timeout(Some(IDLE_TIMEOUT));
let _ = stream.set_write_timeout(Some(IDLE_TIMEOUT));
let mut reader = BufReader::new(stream);
let mut line = Vec::new();
loop {
line.clear();
match (&mut reader)
.take(MAX_LINE as u64 + 3)
.read_until(b'\n', &mut line)
{
Ok(0) | Err(_) => break,
Ok(_) => {}
}
let content = line.strip_suffix(b"\n").unwrap_or(&line);
if content.strip_suffix(b"\r").unwrap_or(content).len() > MAX_LINE {
let _ = writer.write_all(b"{\"error\":\"request too long\"}\n");
break;
}
let text = String::from_utf8_lossy(&line);
let text = text.trim_end_matches(['\n', '\r']);
if text.is_empty() {
continue;
}
let response = match serde_json::from_str::<Request>(&line) {
Ok(req) => handle_request(req, allowed),
let response = match serde_json::from_str::<Request>(text) {
Ok(req) => handle_request(req, roots),
Err(e) => Response::err(format!("bad request: {e}")),
};
let mut out = serde_json::to_string(&response)
.unwrap_or_else(|_| r#"{"error":"serialize"}"#.to_string());
out.push('\n');
@ -168,112 +302,175 @@ fn handle_connection(stream: UnixStream, allowed: &[Allowed]) {
}
}
#[cfg_attr(not(any(unix, test)), allow(dead_code))]
fn handle_request(req: Request, allowed: &[Allowed]) -> Response {
match req {
#[cfg(target_os = "linux")]
fn handle_request(req: Request, roots: &[Root]) -> Response {
let result = match req {
Request::Read { path } => {
let p = PathBuf::from(&path);
if !is_allowed(&p, allowed, false) {
return Response::err(format!("access denied: {path}"));
}
match std::fs::read(&p) {
Ok(data) => Response::OkData {
data_b64: base64::Engine::encode(
&base64::engine::general_purpose::STANDARD,
&data,
),
},
Err(e) => Response::err(e),
}
}
Request::Write { path, data_b64 } => {
let p = PathBuf::from(&path);
if !is_allowed(&p, allowed, true) {
return Response::err(format!("access denied: {path}"));
locate(roots, &path, false).and_then(|(root, rest)| read(root, rest))
}
Request::Write { path, data_b64 } => locate(roots, &path, true).and_then(|(root, rest)| {
let data =
match base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64)
{
Ok(d) => d,
Err(e) => return Response::err(format!("bad base64: {e}")),
};
if let Some(Err(e)) = p.parent().map(std::fs::create_dir_all) {
return Response::err(e);
}
match std::fs::write(&p, &data) {
Ok(()) => Response::Ok,
Err(e) => Response::err(e),
}
}
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64)
.map_err(|e| Response::err(format!("bad base64: {e}")))?;
write(root, rest, &data)
}),
Request::List { path } => {
let p = PathBuf::from(&path);
if !is_allowed(&p, allowed, false) {
return Response::err(format!("access denied: {path}"));
}
match std::fs::read_dir(&p) {
Ok(entries) => {
let mut names = Vec::new();
for entry in entries.flatten() {
if let Some(name) = entry.file_name().to_str() {
names.push(name.to_string());
}
}
names.sort();
Response::OkEntries { entries: names }
}
Err(e) => Response::err(e),
}
}
locate(roots, &path, false).and_then(|(root, rest)| list(root, rest))
}
};
result.unwrap_or_else(|response| response)
}
#[cfg(test)]
#[cfg(target_os = "linux")]
fn read(root: &Root, rest: &Path) -> Result<Response, Response> {
use rustix::fs::{FileType, OFlags};
let fd = open_beneath(root, rest, OFlags::RDONLY | OFlags::NONBLOCK).map_err(Response::err)?;
let stat = rustix::fs::fstat(&fd).map_err(Response::err)?;
if FileType::from_raw_mode(stat.st_mode) != FileType::RegularFile {
return Err(Response::err("not a regular file"));
}
let mut data = Vec::new();
std::fs::File::from(fd)
.take(MAX_FILE as u64 + 1)
.read_to_end(&mut data)
.map_err(Response::err)?;
if data.len() > MAX_FILE {
return Err(Response::err(format!("file larger than {MAX_FILE} bytes")));
}
Ok(Response::OkData {
data_b64: base64::Engine::encode(&base64::engine::general_purpose::STANDARD, &data),
})
}
#[cfg(target_os = "linux")]
fn list(root: &Root, rest: &Path) -> Result<Response, Response> {
use rustix::fs::OFlags;
let fd = open_beneath(root, rest, OFlags::RDONLY | OFlags::DIRECTORY).map_err(Response::err)?;
let mut entries = rustix::fs::Dir::read_from(&fd).map_err(Response::err)?;
let mut names = Vec::new();
while let Some(entry) = entries.read() {
let entry = entry.map_err(Response::err)?;
let Ok(name) = entry.file_name().to_str() else {
continue;
};
if name == "." || name == ".." || name.starts_with(TEMPORARY_PREFIX) {
continue;
}
if names.len() == MAX_ENTRIES {
return Err(Response::err(format!("more than {MAX_ENTRIES} entries")));
}
names.push(name.to_owned());
}
names.sort();
Ok(Response::OkEntries { entries: names })
}
/// Replaces the file `rest` in `root` with `data`: the data is written to a
/// new file in the same directory, flushed and renamed over the name, so
/// the file holds either the old or the new content.
#[cfg(target_os = "linux")]
fn write(root: &Root, rest: &Path, data: &[u8]) -> Result<Response, Response> {
use rustix::fs::{Mode, OFlags};
if data.len() > MAX_FILE {
return Err(Response::err(format!("file larger than {MAX_FILE} bytes")));
}
let name = rest
.file_name()
.ok_or_else(|| Response::err("a write needs a file name"))?;
let parent = open_beneath(
root,
rest.parent().unwrap_or(Path::new("")),
OFlags::RDONLY | OFlags::DIRECTORY,
)
.map_err(Response::err)?;
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
// Unique within this portal by the counter, and across restarts by the
// time of its first write, so a file left by an earlier instance does
// not collide.
static STARTED: std::sync::OnceLock<u128> = std::sync::OnceLock::new();
let started = STARTED.get_or_init(|| {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos())
});
let temporary = format!(
"{TEMPORARY_PREFIX}{}-{started}-{}",
std::process::id(),
NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
);
// A replaced file keeps its permission bits; a new one is private.
let mode = match rustix::fs::statat(&parent, name, rustix::fs::AtFlags::SYMLINK_NOFOLLOW) {
Ok(stat)
if rustix::fs::FileType::from_raw_mode(stat.st_mode)
== rustix::fs::FileType::RegularFile =>
{
stat.st_mode & 0o777
}
_ => 0o600,
};
let file = rustix::fs::openat(
&parent,
temporary.as_str(),
OFlags::WRONLY | OFlags::CREATE | OFlags::EXCL | OFlags::NOFOLLOW | OFlags::CLOEXEC,
Mode::from_raw_mode(0o600),
)
.map_err(Response::err)?;
let _ = rustix::fs::fchmod(&file, Mode::from_raw_mode(mode));
let mut file = std::fs::File::from(file);
let written = file
.write_all(data)
.and_then(|()| file.sync_all())
.and_then(|()| {
rustix::fs::renameat(&parent, temporary.as_str(), &parent, name)
.map_err(std::io::Error::from)
});
if let Err(e) = written {
let _ = rustix::fs::unlinkat(&parent, temporary.as_str(), rustix::fs::AtFlags::empty());
return Err(Response::err(e));
}
let _ = rustix::fs::fsync(&parent);
Ok(Response::Ok)
}
#[cfg(all(test, target_os = "linux"))]
mod tests {
use super::*;
use std::fs;
fn rw(root: impl Into<PathBuf>) -> Allowed {
Allowed {
root: root.into(),
writable: true,
}
fn grant(root: &Path, writable: bool) -> Vec<Root> {
vec![
Root::open(Allowed {
root: root.to_path_buf(),
writable,
})
.unwrap(),
]
}
fn ro(root: impl Into<PathBuf>) -> Allowed {
Allowed {
root: root.into(),
writable: false,
}
fn temp(name: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("wfp_{name}_{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(dir.join("granted")).unwrap();
dir
}
#[test]
fn allowed_path_accepted() {
let allowed = vec![rw("/tmp/weft-test-allowed")];
assert!(is_allowed(
Path::new("/tmp/weft-test-allowed/file.txt"),
&allowed,
false
));
fn path(p: &Path) -> String {
p.to_string_lossy().into_owned()
}
#[test]
fn disallowed_path_rejected() {
let allowed = vec![rw("/tmp/weft-test-allowed")];
assert!(!is_allowed(Path::new("/etc/passwd"), &allowed, false));
fn denied(resp: &Response) -> bool {
matches!(resp, Response::Err { .. })
}
#[test]
fn dotdot_traversal_blocked() {
let allowed = vec![rw("/tmp/weft-test-allowed")];
assert!(!is_allowed(
Path::new("/tmp/weft-test-allowed/../etc/passwd"),
&allowed,
false
));
fn read_req(p: &Path) -> Request {
Request::Read { path: path(p) }
}
#[test]
fn empty_allowlist_rejects_all() {
assert!(!is_allowed(Path::new("/tmp/anything"), &[], false));
fn write_req(p: &Path, data: &[u8]) -> Request {
Request::Write {
path: path(p),
data_b64: base64::Engine::encode(&base64::engine::general_purpose::STANDARD, data),
}
}
#[test]
@ -281,133 +478,277 @@ mod tests {
let args: Vec<String> = vec![
"--allow".into(),
"/tmp/a".into(),
"--allow".into(),
"/tmp/b".into(),
"--allow-read".into(),
"/tmp/c".into(),
];
let result = parse_allowed(&args);
assert_eq!(result, vec![rw("/tmp/a"), rw("/tmp/b")]);
let args: Vec<String> = vec!["--allow-read".into(), "/tmp/c".into()];
assert_eq!(parse_allowed(&args), vec![ro("/tmp/c")]);
}
#[test]
fn handle_request_read_denied() {
let resp = handle_request(
Request::Read {
path: "/etc/shadow".into(),
assert_eq!(
parse_allowed(&args),
vec![
Allowed {
root: "/tmp/a".into(),
writable: true
},
&[rw("/tmp/safe")],
);
let json = serde_json::to_string(&resp).unwrap();
assert!(json.contains("access denied"));
}
#[test]
fn handle_request_read_roundtrip() {
use std::fs;
let dir = std::env::temp_dir().join(format!("wfp_test_{}", std::process::id()));
let _ = fs::create_dir_all(&dir);
let file = dir.join("hello.txt");
fs::write(&file, b"hello world").unwrap();
let allowed = vec![rw(dir.clone())];
let resp = handle_request(
Request::Read {
path: file.to_string_lossy().into(),
Allowed {
root: "/tmp/c".into(),
writable: false
},
&allowed,
]
);
let json = serde_json::to_string(&resp).unwrap();
assert!(json.contains("data_b64"));
if let Response::OkData { data_b64 } = resp {
let decoded =
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &data_b64)
.unwrap();
assert_eq!(decoded, b"hello world");
} else {
panic!("expected OkData");
}
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn handle_request_list() {
use std::fs;
let dir = std::env::temp_dir().join(format!("wfp_list_{}", std::process::id()));
let _ = fs::create_dir_all(&dir);
fs::write(dir.join("b.txt"), b"").unwrap();
fs::write(dir.join("a.txt"), b"").unwrap();
fn paths_must_name_a_place_inside_a_granted_directory_directly() {
let root = Path::new("/srv/granted");
assert_eq!(
relative_to(Path::new("/srv/granted/a/b"), root),
Some(Path::new("a/b"))
);
assert_eq!(
relative_to(Path::new("/srv/granted"), root),
Some(Path::new(""))
);
for outside in [
"/srv/grantedx/a",
"/srv/granted/../x",
"/srv/granted/a/../../x",
"/etc",
"/srv/granted/a/./b",
"/srv/granted/a/.",
"/srv/granted/a/",
"/srv/granted//a",
"srv/granted/a",
] {
assert_eq!(relative_to(Path::new(outside), root), None, "{outside}");
}
}
let allowed = vec![rw(dir.clone())];
let resp = handle_request(
#[test]
fn reads_writes_and_lists_inside_the_directory() {
let dir = temp("roundtrip");
let granted = dir.join("granted");
let roots = grant(&granted, true);
assert!(matches!(
handle_request(write_req(&granted.join("b.txt"), b"hello"), &roots),
Response::Ok
));
fs::write(granted.join("a.txt"), b"").unwrap();
match handle_request(read_req(&granted.join("b.txt")), &roots) {
Response::OkData { data_b64 } => assert_eq!(
base64::Engine::decode(&base64::engine::general_purpose::STANDARD, data_b64)
.unwrap(),
b"hello"
),
_ => panic!("expected data"),
}
match handle_request(
Request::List {
path: dir.to_string_lossy().into(),
path: path(&granted),
},
&allowed,
);
if let Response::OkEntries { entries } = resp {
assert_eq!(entries, vec!["a.txt", "b.txt"]);
} else {
panic!("expected OkEntries");
&roots,
) {
Response::OkEntries { entries } => assert_eq!(entries, ["a.txt", "b.txt"]),
_ => panic!("expected entries"),
}
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn handle_request_write_creates_parent_dirs() {
use std::fs;
let dir = std::env::temp_dir().join(format!("wfp_write_{}", std::process::id()));
fn links_cannot_lead_out_of_the_directory() {
let dir = temp("links");
let granted = dir.join("granted");
fs::write(dir.join("secret.txt"), b"secret").unwrap();
fs::create_dir(granted.join("sub")).unwrap();
std::os::unix::fs::symlink("/", granted.join("root")).unwrap();
std::os::unix::fs::symlink("../secret.txt", granted.join("up")).unwrap();
std::os::unix::fs::symlink(&dir, granted.join("parent")).unwrap();
let roots = grant(&granted, true);
for escape in [
granted.join("root/etc/passwd"),
granted.join("up"),
granted.join("parent/secret.txt"),
// The check this replaced normalised `link/..` away and then
// followed the link.
granted.join("root/../secret.txt"),
] {
assert!(
denied(&handle_request(read_req(&escape), &roots)),
"{}",
escape.display()
);
if escape != granted.join("up") {
assert!(
denied(&handle_request(write_req(&escape, b"x"), &roots)),
"{}",
escape.display()
);
}
}
// A write to a name that is a link replaces the link, inside the
// directory, rather than writing where it points.
match handle_request(read_req(&granted.join("root/etc/passwd")), &roots) {
Response::Err { error } => assert!(error.contains("leads out"), "{error}"),
_ => panic!("read through a link out of the directory"),
}
assert!(matches!(
handle_request(write_req(&granted.join("up"), b"x"), &roots),
Response::Ok
));
assert!(fs::symlink_metadata(granted.join("up")).unwrap().is_file());
assert_eq!(fs::read(dir.join("secret.txt")).unwrap(), b"secret");
// A link that stays inside is followed.
std::os::unix::fs::symlink("sub", granted.join("inside")).unwrap();
assert!(matches!(
handle_request(write_req(&granted.join("inside/f"), b"ok"), &roots),
Response::Ok
));
assert_eq!(fs::read(granted.join("sub/f")).unwrap(), b"ok");
let _ = fs::remove_dir_all(&dir);
let nested = dir.join("sub").join("deep").join("file.txt");
let data = base64::Engine::encode(
}
#[test]
fn a_directory_swapped_for_a_link_mid_operation_cannot_lead_out() {
let dir = temp("race");
let granted = dir.join("granted");
fs::create_dir(dir.join("outside")).unwrap();
fs::write(dir.join("outside/f"), b"secret").unwrap();
fs::create_dir(granted.join("real")).unwrap();
fs::write(granted.join("real/f"), b"inside").unwrap();
let roots = grant(&granted, true);
// `swap` is a link, replaced in a loop, alternately to the inside
// directory and out of the granted one, absolute and relative.
std::os::unix::fs::symlink("real", granted.join("swap")).unwrap();
let stop = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let swapper = {
let (granted, outside, stop) = (granted.clone(), dir.join("outside"), stop.clone());
std::thread::spawn(move || {
let targets = [PathBuf::from("real"), outside, PathBuf::from("../outside")];
for target in targets.iter().cycle() {
if stop.load(std::sync::atomic::Ordering::Relaxed) {
break;
}
let _ = std::os::unix::fs::symlink(target, granted.join("next"));
let _ = fs::rename(granted.join("next"), granted.join("swap"));
}
})
};
let (mut inside, mut refused) = (0, 0);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(20);
while (inside < 50 || refused < 50) && std::time::Instant::now() < deadline {
match handle_request(read_req(&granted.join("swap/f")), &roots) {
Response::OkData { data_b64 } => {
let data = base64::Engine::decode(
&base64::engine::general_purpose::STANDARD,
b"nested content",
data_b64,
)
.unwrap();
assert_eq!(data, b"inside", "a read left the directory");
inside += 1;
}
Response::Err { error } if error.contains("leads out") => refused += 1,
_ => {}
}
let _ = handle_request(write_req(&granted.join("swap/f"), b"inside"), &roots);
}
stop.store(true, std::sync::atomic::Ordering::Relaxed);
swapper.join().unwrap();
assert_eq!(fs::read(dir.join("outside/f")).unwrap(), b"secret");
assert!(
inside >= 50 && refused >= 50,
"the race was not exercised: {inside} reads inside, {refused} refused"
);
let allowed = vec![rw(dir.clone())];
let resp = handle_request(
Request::Write {
path: nested.to_string_lossy().into(),
data_b64: data,
},
&allowed,
);
assert!(matches!(resp, Response::Ok), "expected Ok response");
assert_eq!(fs::read(&nested).unwrap(), b"nested content");
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn a_replaced_file_keeps_its_mode() {
use std::os::unix::fs::PermissionsExt;
let dir = temp("mode");
let granted = dir.join("granted");
fs::write(granted.join("shared.txt"), b"old").unwrap();
fs::set_permissions(
granted.join("shared.txt"),
fs::Permissions::from_mode(0o644),
)
.unwrap();
let roots = grant(&granted, true);
assert!(matches!(
handle_request(write_req(&granted.join("shared.txt"), b"new"), &roots),
Response::Ok
));
let mode = fs::metadata(granted.join("shared.txt"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o644);
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn read_only_directories_refuse_writes() {
use std::fs;
let dir = std::env::temp_dir().join(format!("wfp_ro_{}", std::process::id()));
let dir = temp("ro");
let granted = dir.join("granted");
fs::write(granted.join("kept.txt"), b"original").unwrap();
let roots = grant(&granted, false);
assert!(denied(&handle_request(
write_req(&granted.join("kept.txt"), b"x"),
&roots
)));
assert_eq!(fs::read(granted.join("kept.txt")).unwrap(), b"original");
assert!(matches!(
handle_request(read_req(&granted.join("kept.txt")), &roots),
Response::OkData { .. }
));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
fs::write(dir.join("kept.txt"), b"original").unwrap();
let allowed = vec![ro(dir.clone())];
let data = base64::Engine::encode(&base64::engine::general_purpose::STANDARD, b"x");
let resp = handle_request(
Request::Write {
path: dir.join("kept.txt").to_string_lossy().into(),
data_b64: data,
},
&allowed,
);
assert!(
serde_json::to_string(&resp)
}
#[test]
fn writes_create_no_directories_and_leave_no_temporary_files() {
let dir = temp("nodirs");
let granted = dir.join("granted");
let roots = grant(&granted, true);
assert!(denied(&handle_request(
write_req(&granted.join("missing/f"), b"x"),
&roots
)));
assert!(!granted.join("missing").exists());
assert!(denied(&handle_request(write_req(&granted, b"x"), &roots)));
let left: Vec<_> = fs::read_dir(&granted).unwrap().flatten().collect();
assert!(left.is_empty(), "{left:?}");
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn oversized_files_are_refused() {
let dir = temp("size");
let granted = dir.join("granted");
fs::File::create(granted.join("big"))
.unwrap()
.contains("access denied")
);
assert_eq!(fs::read(dir.join("kept.txt")).unwrap(), b"original");
let resp = handle_request(
Request::Read {
path: dir.join("kept.txt").to_string_lossy().into(),
},
&allowed,
);
assert!(matches!(resp, Response::OkData { .. }));
.set_len(MAX_FILE as u64 + 1)
.unwrap();
let roots = grant(&granted, true);
assert!(denied(&handle_request(
read_req(&granted.join("big")),
&roots
)));
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn overlong_requests_end_the_connection() {
let dir = temp("line");
let granted = dir.join("granted");
let roots = grant(&granted, true);
let (client, server) = std::os::unix::net::UnixStream::pair().unwrap();
let serve = std::thread::spawn(move || handle_connection(server, &roots));
let mut writer = client.try_clone().unwrap();
let sent = std::thread::spawn(move || {
let _ = writer.write_all(&vec![b'x'; MAX_LINE + 64]);
});
let mut reply = String::new();
BufReader::new(client).read_line(&mut reply).unwrap();
serve.join().unwrap();
let _ = sent.join();
assert!(reply.contains("request too long"), "{reply}");
let _ = fs::remove_dir_all(&dir);
}
}

View file

@ -323,10 +323,6 @@ fn run_module(
}
}
if let Ok(portal_socket) = std::env::var("WEFT_FILE_PORTAL_SOCKET") {
ctx_builder.env("WEFT_FILE_PORTAL_SOCKET", &portal_socket);
}
for dir in preopen {
let (dir_perms, file_perms) = match dir.access {
Access::Read => (DirPerms::READ, FilePerms::READ),

View file

@ -32,7 +32,7 @@ Package management CLI. Subcommands: `check` (validate wapp.toml, entries and pa
### weft-file-portal
Per-session file proxy. Runs as a separate process with a path allowlist derived from the session's directory grants; read-only grants (`--allow-read`) refuse writes. Paths are checked lexically, so `..` is blocked but symbolic links are followed. Accepts JSON-lines requests over a Unix socket. Components cannot currently reach that socket, because the runtime grants them no socket access.
Per-session file proxy. Runs as a separate process with the session's directory grants; read-only grants (`--allow-read`) refuse writes. Every operation is resolved beneath the granted directory's descriptor by the kernel (see security.md), with bounded file, listing and request sizes. Accepts JSON-lines requests over a Unix socket. Components cannot currently reach that socket, because the runtime grants them no socket access.
### weft-mount-helper
@ -166,5 +166,4 @@ A package directory left empty by the move is removed. Nothing is moved, and `we
| `WEFT_APPD_WS_PORT` | `7410` | WebSocket port for weft-appd |
| `WEFT_EGL_RENDERING` | — | Set to `1` to use EGL rendering in Servo shell |
| `WEFT_DISABLE_CGROUP` | — | Set to disable systemd-run cgroup wrapping |
| `WEFT_FILE_PORTAL_SOCKET` | — | Path forwarded to app runtime for file portal |
| `XDG_RUNTIME_DIR` | — | Standard XDG runtime dir (sockets written here) |

View file

@ -18,7 +18,7 @@ Each app session runs as a separate OS process (`weft-runtime`). When systemd is
## Filesystem Isolation
Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes, but checks paths lexically: it blocks `..` traversal, not symbolic links. Components cannot currently reach its socket, since the runtime grants them no socket access.
Apps access the filesystem only through WASI preopened directories. Each capability maps to a specific host path preopened at a fixed guest path. The `weft-file-portal` process applies the same directory grants and access modes. It opens each granted directory once and resolves every request beneath that descriptor with `openat2(RESOLVE_BENEATH)`, so the kernel keeps the operation inside the directory: `..` and links pointing outside are refused, also when a link is swapped in while the operation runs; links that stay inside are followed. This needs Linux 5.6 or later; without `openat2` every operation fails. Requests must name an absolute path without `.`, `..` or empty components; files are limited to 16 MiB, listings to 10,000 entries, requests to one line of about 22 MiB, and the portal to 4 connections at a time, each closed when it sends or accepts no data for 60 seconds. Writes replace a file atomically, keeping its permission bits but not its owner, extended attributes or hard links (a write to a name that is a link replaces the link), and never create directories. A granted directory that cannot be opened when the portal starts is not served. Components cannot currently reach its socket, since the runtime grants them no socket access, and the socket's path is not passed to them.
## Package Signing and Trust