From 044634692f93e0a2a90417b607e4c86008657249 Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 18:23:07 +0000 Subject: [PATCH] fix(runtime): bound component memory and app messages The runtime set no limits of its own: - A component could grow its linear memory until the host ran out, with no ceiling at all where systemd is unavailable. - An app message could contain line breaks and split into several messages. - An overlong message from weft-appd was buffered without bound. - A closed connection looked the same as an empty one. The Wasmtime store now has a limiter: linear memory is capped at 256 MiB by default (--max-memory-mib changes it), and tables and instances are bounded too. A memory that would grow past the limit does not grow, and the component sees an allocation failure. App messages are single lines of at most 64 KiB, the limit weft-appd applies. A longer message, or one containing a line break, is refused. A longer line from weft-appd ends the connection, and sending afterwards reports that it is closed. The systemd scope also gets TasksMax=64. The grants probe component now also tries a 300 MiB allocation and the two refused messages. tests/runtime/check_grants.py checks that the allocation fails at the default limit and succeeds with a 512 MiB limit. --- crates/weft-appd/src/runtime.rs | 2 + crates/weft-runtime/src/ipc.rs | 175 ++++++++++++++++++++ crates/weft-runtime/src/main.rs | 101 +++++------ docs/architecture.md | 2 +- docs/security.md | 2 +- tests/components/grants-probe/src/main.rs | 19 ++- tests/components/grants-probe/wit/world.wit | 1 + tests/runtime/check_grants.py | 15 +- 8 files changed, 251 insertions(+), 66 deletions(-) create mode 100644 crates/weft-runtime/src/ipc.rs diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 6f9cce9..13c9ce2 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -163,6 +163,8 @@ const SYSTEMD_SCOPE_ARGS: &[&str] = &[ "CPUQuota=200%", "-p", "MemoryMax=512M", + "-p", + "TasksMax=64", ]; fn systemd_cgroup_available() -> bool { diff --git a/crates/weft-runtime/src/ipc.rs b/crates/weft-runtime/src/ipc.rs new file mode 100644 index 0000000..2900e56 --- /dev/null +++ b/crates/weft-runtime/src/ipc.rs @@ -0,0 +1,175 @@ +//! The component's connection to its session's IPC relay in weft-appd. +//! +//! Messages are single lines. A message is at most `MAX_MESSAGE` bytes, the +//! limit weft-appd applies to app messages, and cannot contain a line break, +//! which would split it into several messages. Received data is buffered +//! up to one message; a peer that sends a longer line, or closes the +//! connection, ends it for good. + +use std::io::{Read, Write}; +use std::os::unix::net::UnixStream; + +/// The longest message in either direction, in bytes. +pub const MAX_MESSAGE: usize = 64 * 1024; + +/// Checks a message the component wants to send. +pub fn check_payload(payload: &str) -> Result<(), String> { + if payload.len() > MAX_MESSAGE { + return Err(format!("message exceeds {MAX_MESSAGE} bytes")); + } + if payload.contains(['\n', '\r']) { + return Err("message contains a line break".to_owned()); + } + Ok(()) +} + +pub struct IpcState { + socket: UnixStream, + recv_buf: Vec, + /// Why the connection ended, once it has. + closed: Option<&'static str>, +} + +impl IpcState { + pub fn connect(path: &str) -> Option { + let socket = UnixStream::connect(path).ok()?; + Self::new(socket) + } + + fn new(socket: UnixStream) -> Option { + socket.set_nonblocking(true).ok()?; + Some(Self { + socket, + recv_buf: Vec::new(), + closed: None, + }) + } + + pub fn send(&mut self, payload: &str) -> Result<(), String> { + check_payload(payload)?; + if let Some(reason) = self.closed { + return Err(format!("IPC connection {reason}")); + } + let _ = self.socket.set_nonblocking(false); + let mut line = payload.to_owned(); + line.push('\n'); + let result = self + .socket + .write_all(line.as_bytes()) + .map_err(|e| e.to_string()); + let _ = self.socket.set_nonblocking(true); + result + } + + /// The next complete message, if one has arrived. + pub fn recv(&mut self) -> Option { + if let Some(message) = self.take_line() { + return Some(message); + } + if self.closed.is_some() { + return None; + } + let mut chunk = [0u8; 4096]; + loop { + match self.socket.read(&mut chunk) { + Ok(0) => { + self.close("closed by weft-appd"); + break; + } + Ok(n) => { + self.recv_buf.extend_from_slice(&chunk[..n]); + if self.recv_buf.contains(&b'\n') { + break; + } + if self.recv_buf.len() > MAX_MESSAGE { + self.close("ended by a message over the size limit"); + self.recv_buf.clear(); + break; + } + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(_) => { + self.close("failed"); + break; + } + } + } + self.take_line() + } + + fn close(&mut self, reason: &'static str) { + tracing::warn!(reason, "IPC connection ended"); + self.closed = Some(reason); + } + + /// Removes the first complete line from the buffer. A line that is not + /// UTF-8, or longer than a message, is dropped. + fn take_line(&mut self) -> Option { + loop { + let pos = self.recv_buf.iter().position(|&b| b == b'\n')?; + let raw: Vec = self.recv_buf.drain(..=pos).collect(); + let text = raw.strip_suffix(b"\n").unwrap_or(&raw); + let text = text.strip_suffix(b"\r").unwrap_or(text); + if text.len() > MAX_MESSAGE { + continue; + } + if let Ok(message) = String::from_utf8(text.to_vec()) { + return Some(message); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn pair() -> (IpcState, UnixStream) { + let (ours, theirs) = UnixStream::pair().unwrap(); + (IpcState::new(ours).unwrap(), theirs) + } + + #[test] + fn payloads_that_would_break_framing_are_refused() { + assert!(check_payload("ok").is_ok()); + assert!(check_payload("two\nmessages").is_err()); + assert!(check_payload("carriage\rreturn").is_err()); + assert!(check_payload(&"x".repeat(MAX_MESSAGE)).is_ok()); + assert!(check_payload(&"x".repeat(MAX_MESSAGE + 1)).is_err()); + let (mut ipc, _peer) = pair(); + assert!(ipc.send("a\nb").is_err()); + } + + #[test] + fn messages_arrive_one_at_a_time() { + let (mut ipc, mut peer) = pair(); + assert_eq!(ipc.recv(), None); + peer.write_all(b"first\nsecond\r\npart").unwrap(); + assert_eq!(ipc.recv().as_deref(), Some("first")); + assert_eq!(ipc.recv().as_deref(), Some("second")); + assert_eq!(ipc.recv(), None); + peer.write_all(b"ial\n").unwrap(); + assert_eq!(ipc.recv().as_deref(), Some("partial")); + } + + #[test] + fn an_overlong_line_ends_the_connection_without_unbounded_buffering() { + let (mut ipc, mut peer) = pair(); + peer.write_all(&vec![b'x'; MAX_MESSAGE + 4097]).unwrap(); + while ipc.closed.is_none() { + assert_eq!(ipc.recv(), None); + } + assert!(ipc.recv_buf.len() <= MAX_MESSAGE + 4096); + assert!(ipc.send("after").is_err()); + } + + #[test] + fn a_closed_connection_is_reported_to_senders() { + let (mut ipc, peer) = pair(); + drop(peer); + assert_eq!(ipc.recv(), None); + let error = ipc.send("hello").unwrap_err(); + assert!(error.contains("closed"), "{error}"); + } +} diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index 1fe5d05..d3f589d 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -3,6 +3,11 @@ use std::path::PathBuf; use anyhow::Context; mod grants; +#[cfg(feature = "wasmtime-runtime")] +mod ipc; + +/// The default limit on a component's linear memory, in MiB. +const DEFAULT_MAX_MEMORY_MIB: usize = 256; use grants::{Grants, Preopen}; @@ -18,7 +23,8 @@ fn main() -> anyhow::Result<()> { if args.len() < 3 { anyhow::bail!( "usage: weft-runtime --module PATH \ - [--preopen HOST::GUEST::ro|rw]... [--grant CAPABILITY]... [--ipc-socket PATH]" + [--preopen HOST::GUEST::ro|rw]... [--grant CAPABILITY]... [--ipc-socket PATH] \ + [--max-memory-mib N]" ); } let app_id = &args[1]; @@ -30,6 +36,7 @@ fn main() -> anyhow::Result<()> { let mut grants = Grants::default(); let mut ipc_socket: Option = None; let mut module: Option = None; + let mut max_memory_mib = DEFAULT_MAX_MEMORY_MIB; let mut i = 3usize; while i < args.len() { @@ -49,6 +56,17 @@ fn main() -> anyhow::Result<()> { args.get(i).context("--module requires an argument")?, )); } + "--max-memory-mib" => { + i += 1; + let value = args + .get(i) + .context("--max-memory-mib requires an argument")?; + max_memory_mib = value + .parse::() + .ok() + .filter(|&mib| mib > 0 && mib <= 1 << 20) + .with_context(|| format!("invalid --max-memory-mib: {value}"))?; + } "--ipc-socket" => { i += 1; ipc_socket = Some( @@ -72,7 +90,13 @@ fn main() -> anyhow::Result<()> { let wasm_path = module.context("--module is required")?; tracing::info!(session_id, %app_id, wasm = %wasm_path.display(), "executing module"); - run_module(&wasm_path, &preopen, grants, ipc_socket.as_deref())?; + run_module( + &wasm_path, + &preopen, + grants, + ipc_socket.as_deref(), + max_memory_mib << 20, + )?; tracing::info!(session_id, %app_id, "exiting"); Ok(()) @@ -94,6 +118,7 @@ fn run_module( _preopen: &[Preopen], _grants: Grants, _ipc_socket: Option<&str>, + _max_memory: usize, ) -> anyhow::Result<()> { anyhow::bail!( "weft-runtime was built without the wasmtime-runtime feature and cannot execute components" @@ -106,6 +131,7 @@ fn run_module( preopen: &[Preopen], grants: Grants, ipc_socket: Option<&str>, + max_memory: usize, ) -> anyhow::Result<()> { use std::sync::{Arc, Mutex}; use wasmtime::{ @@ -118,63 +144,10 @@ fn run_module( }; use weft_ipc_types::capability::Access; - struct IpcState { - socket: std::os::unix::net::UnixStream, - recv_buf: Vec, - } - - impl IpcState { - fn connect(path: &str) -> Option { - let socket = std::os::unix::net::UnixStream::connect(path).ok()?; - socket.set_nonblocking(true).ok()?; - Some(Self { - socket, - recv_buf: Vec::new(), - }) - } - - fn send(&mut self, payload: &str) -> Result<(), String> { - use std::io::Write; - let _ = self.socket.set_nonblocking(false); - let mut line = payload.to_owned(); - line.push('\n'); - let result = self - .socket - .write_all(line.as_bytes()) - .map_err(|e| e.to_string()); - let _ = self.socket.set_nonblocking(true); - result - } - - fn recv(&mut self) -> Option { - use std::io::Read; - let mut chunk = [0u8; 4096]; - loop { - match self.socket.read(&mut chunk) { - Ok(0) => break, - Ok(n) => self.recv_buf.extend_from_slice(&chunk[..n]), - Err(e) - if e.kind() == std::io::ErrorKind::WouldBlock - || e.kind() == std::io::ErrorKind::TimedOut => - { - break; - } - Err(_) => break, - } - } - if let Some(pos) = self.recv_buf.iter().position(|&b| b == b'\n') { - let raw: Vec = self.recv_buf.drain(..=pos).collect(); - return String::from_utf8(raw) - .ok() - .map(|s| s.trim_end_matches('\n').trim_end_matches('\r').to_owned()); - } - None - } - } - struct State { ctx: WasiCtx, table: ResourceTable, + limits: wasmtime::StoreLimits, } impl IoView for State { @@ -199,7 +172,7 @@ fn run_module( let grants = Arc::new(grants); let mut linker: Linker = Linker::new(&engine); add_to_linker_sync(&mut linker).context("add WASI to linker")?; - let ipc_state: Arc>> = Arc::new(Mutex::new(None)); + let ipc_state: Arc>> = Arc::new(Mutex::new(None)); { let ipc_send = Arc::clone(&ipc_state); @@ -223,6 +196,9 @@ fn run_module( move |_: wasmtime::StoreContextMut<'_, State>, (payload,): (String,)| -> wasmtime::Result<(Result<(), String>,)> { + if let Err(e) = ipc::check_payload(&payload) { + return Ok((Err(e),)); + } let mut guard = ipc_send.lock().unwrap_or_else(|p| p.into_inner()); match guard.as_mut() { Some(ipc) => Ok((ipc.send(&payload),)), @@ -316,7 +292,7 @@ fn run_module( if let Some(socket_path) = ipc_socket { ctx_builder.env("WEFT_IPC_SOCKET", socket_path); - if let Some(ipc) = IpcState::connect(socket_path) { + if let Some(ipc) = ipc::IpcState::connect(socket_path) { *ipc_state.lock().unwrap_or_else(|p| p.into_inner()) = Some(ipc); } else { tracing::warn!("weft:app/ipc: could not connect to IPC socket {socket_path}"); @@ -343,8 +319,16 @@ fn run_module( State { ctx, table: ResourceTable::new(), + // A memory or table that would grow past the limit does not + // grow: the component sees an allocation failure. + limits: wasmtime::StoreLimitsBuilder::new() + .memory_size(max_memory) + .table_elements(100_000) + .instances(1_000) + .build(), }, ); + store.limiter(|state| &mut state.limits); let command = Command::instantiate(&mut store, &component, &linker).context("instantiate component")?; @@ -602,6 +586,7 @@ mod tests { &[], Grants::default(), None, + DEFAULT_MAX_MEMORY_MIB << 20, ) .expect_err("a build without wasmtime-runtime must not report success"); assert!(err.to_string().contains("wasmtime-runtime")); diff --git a/docs/architecture.md b/docs/architecture.md index ee92a31..93af5ec 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -46,7 +46,7 @@ systemd ├── weft-servo-shell (user, after compositor) └── weft-appd (user, after compositor + servo-shell) └── per-session: - ├── weft-runtime --ipc-socket --module [--preopen HOST::GUEST::ro|rw]... [--grant CAP]... + ├── weft-runtime --ipc-socket --module [--preopen HOST::GUEST::ro|rw]... [--grant CAP]... [--max-memory-mib N] ├── weft-app-shell --ui └── weft-file-portal [--allow ...] [--allow-read ...] ``` diff --git a/docs/security.md b/docs/security.md index 09a6b4a..4a72e9a 100644 --- a/docs/security.md +++ b/docs/security.md @@ -12,7 +12,7 @@ Installing a package does not yet ask the user to approve its capabilities; ever ## Process Isolation -Each app session runs as a separate OS process (`weft-runtime`). When systemd is available, the process is wrapped in a systemd scope (`weft-apps.slice`) with `CPUQuota=200%` and `MemoryMax=512M`. +Each app session runs as a separate OS process (`weft-runtime`). When systemd is available, the process is wrapped in a systemd scope (`weft-apps.slice`) with `CPUQuota=200%`, `MemoryMax=512M` and `TasksMax=64`. Independently of systemd, the runtime limits the component's linear memory to 256 MiB (`--max-memory-mib` changes it): a memory that would grow past the limit does not grow, and the component sees an allocation failure. App messages over `weft:app/ipc` are single lines of at most 64 KiB, the limit weft-appd applies; a component's message that is longer or contains a line break is refused, and a longer line from weft-appd ends the connection, after which sending reports that it is closed. CPU time within the quota is not limited per component. ## Filesystem Isolation diff --git a/tests/components/grants-probe/src/main.rs b/tests/components/grants-probe/src/main.rs index ba18195..80b7420 100644 --- a/tests/components/grants-probe/src/main.rs +++ b/tests/components/grants-probe/src/main.rs @@ -1,6 +1,6 @@ -//! Test component that calls every grant-controlled operation once and -//! prints one `PROBE ok ` or `PROBE err ` -//! line for each. +//! Test component that calls every grant-controlled operation once, and +//! probes the runtime's memory and message limits, then prints one +//! `PROBE ok ` or `PROBE err ` line for each. //! //! Fetch targets are read from `/probe/targets.txt` (` ` per //! line), a directory the harness preopens read-only next to the grants @@ -13,7 +13,10 @@ wit_bindgen::generate!({ generate_all, }); -use weft::app::{clipboard, fetch, notifications, notify}; +use weft::app::{clipboard, fetch, ipc, notifications, notify}; + +/// An allocation larger than the runtime's default memory limit. +const MEMORY_PROBE: usize = 300 << 20; fn report(name: &str, result: Result) { match result { @@ -49,6 +52,14 @@ fn main() { ); } } + let mut big: Vec = Vec::new(); + report( + "memory-grow", + big.try_reserve(MEMORY_PROBE).map(|()| big.capacity()), + ); + drop(big); + report("ipc-newline", ipc::send("two\nmessages")); + report("ipc-oversize", ipc::send(&"x".repeat(64 * 1024 + 1))); report( "fetch-file-scheme", fetch::fetch("file:///etc/hostname", "GET", &[], None).map(|r| r.status), diff --git a/tests/components/grants-probe/wit/world.wit b/tests/components/grants-probe/wit/world.wit index 0949bf8..14a23bb 100644 --- a/tests/components/grants-probe/wit/world.wit +++ b/tests/components/grants-probe/wit/world.wit @@ -5,4 +5,5 @@ world app { import weft:app/fetch@0.1.0; import weft:app/notifications@0.1.0; import weft:app/clipboard@0.1.0; + import weft:app/ipc@0.1.0; } diff --git a/tests/runtime/check_grants.py b/tests/runtime/check_grants.py index bb1abc5..2ef7bfb 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -18,7 +18,10 @@ scenario passes a different set of `--grant` and `--preopen` arguments: status (200, 404, and 302 without following the redirect to another host); requests to another host, a forged `Host` header, a method that injects a request line and a `file:` URL are refused; -- a wildcard fetch grant: the other host is reachable. +- a wildcard fetch grant: the other host is reachable; +- limits: an allocation beyond the default memory limit (256 MiB) fails + inside the component, and succeeds with `--max-memory-mib 512`; app + messages longer than 64 KiB or containing a line break are refused. It also checks that the runtime refuses, with a specific error, filesystem capabilities passed as `--grant`, unknown capabilities and preopens @@ -147,11 +150,19 @@ def check(runner, data): _, probes = runner.run("--grant", "net:fetch:*") expect(probes, "fetch-other-host", True, contains="200") + _, probes = runner.run() + expect(probes, "memory-grow", False) + expect(probes, "ipc-newline", False, contains="line break") + expect(probes, "ipc-oversize", False, contains="exceeds") + _, probes = runner.run("--max-memory-mib", "512") + expect(probes, "memory-grow", True) + for arguments, message in ( (["--grant", "fs:rw:app-data"], "is not granted through --grant"), (["--preopen", f"{data}::/data"], "--preopen expects HOST::GUEST::ro|rw"), (["--preopen", f"{data}::/data::wx"], "--preopen mode must be ro or rw"), - (["--grant", "sys:everything"], "unknown capability 'sys:everything'")): + (["--grant", "sys:everything"], "unknown capability 'sys:everything'"), + (["--max-memory-mib", "0"], "invalid --max-memory-mib")): result, _ = runner.run(*arguments, expect_success=False) if result.returncode == 0 or message not in result.stderr: raise AssertionError(f"runtime did not refuse {' '.join(arguments)} with "