diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 238e393..0b35827 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -185,6 +185,8 @@ const SYSTEMD_SCOPE_ARGS: &[&str] = &[ "CPUQuota=200%", "-p", "MemoryMax=512M", + "-p", + "TasksMax=512", ]; 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..998ac4e --- /dev/null +++ b/crates/weft-runtime/src/ipc.rs @@ -0,0 +1,226 @@ +//! 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; +/// How long one write of a message may block before the connection is +/// given up. +const SEND_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); + +/// 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 mut line = payload.to_owned(); + line.push('\n'); + let result = self + .socket + .set_nonblocking(false) + .and_then(|()| self.socket.set_write_timeout(Some(SEND_TIMEOUT))) + .and_then(|()| self.socket.write_all(line.as_bytes())); + let restored = self.socket.set_nonblocking(true); + match result.and(restored) { + Ok(()) => Ok(()), + // Part of the line may have been written; anything sent after it + // would join it, so the connection ends. + Err(e) => { + self.close("failed"); + Err(e.to_string()) + } + } + } + + /// 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 + 2 { + self.overlong(); + 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() + } + + /// Ends the connection for both sides: weft-appd sees it close and + /// ends the session, as it does when the component closes it. + fn close(&mut self, reason: &'static str) { + tracing::warn!(reason, "IPC connection ended"); + self.closed = Some(reason); + let _ = self.socket.shutdown(std::net::Shutdown::Both); + } + + fn overlong(&mut self) { + self.close("ended by a message over the size limit"); + self.recv_buf.clear(); + } + + /// Removes the first complete line from the buffer. A line that is not + /// UTF-8 is dropped; one longer than a message ends the connection. + 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 { + self.overlong(); + return None; + } + match String::from_utf8(text.to_vec()) { + Ok(message) => return Some(message), + Err(_) => tracing::warn!("IPC message that is not UTF-8 dropped"), + } + } + } +} + +#[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()); + // The peer sees the connection end. + let mut rest = Vec::new(); + assert_eq!(peer.read_to_end(&mut rest).unwrap(), 0); + } + + #[test] + fn a_message_at_the_limit_arrives_and_one_over_it_ends_the_connection() { + let (mut ipc, mut peer) = pair(); + let mut at_limit = vec![b'x'; MAX_MESSAGE]; + at_limit.push(b'\n'); + peer.write_all(&at_limit).unwrap(); + let mut received = None; + while received.is_none() { + received = ipc.recv(); + } + assert_eq!(received.map(|m| m.len()), Some(MAX_MESSAGE)); + // One byte over, with its newline in the same read. + let mut over = vec![b'x'; MAX_MESSAGE + 1]; + over.extend_from_slice(b"\nnext\n"); + peer.write_all(&over).unwrap(); + while ipc.closed.is_none() { + assert_eq!(ipc.recv(), None); + } + assert_eq!(ipc.recv(), None); + } + + #[test] + fn a_line_that_is_not_utf8_is_dropped() { + let (mut ipc, mut peer) = pair(); + peer.write_all(b"\xff\xfe\nafter\n").unwrap(); + assert_eq!(ipc.recv().as_deref(), Some("after")); + } + + #[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/limits.rs b/crates/weft-runtime/src/limits.rs new file mode 100644 index 0000000..ab50324 --- /dev/null +++ b/crates/weft-runtime/src/limits.rs @@ -0,0 +1,127 @@ +//! The memory and table limits of a component's store. +//! +//! The limit on linear memory covers all of a component's memories +//! together: a component made of several core modules cannot multiply it +//! by declaring more memories. A memory or table that would grow past its +//! limit does not grow, and the component sees an allocation failure. + +/// The most table elements across all of a component's tables. +const MAX_TABLE_ELEMENTS: usize = 1_000_000; +/// The most core instances, memories and tables in one store. +const MAX_INSTANCES: usize = 1_000; +const MAX_MEMORIES: usize = 100; +const MAX_TABLES: usize = 1_000; + +pub struct Limits { + max_memory: usize, + memory: usize, + table_elements: usize, +} + +impl Limits { + /// Limits a store's linear memory to `max_memory` bytes in total. + pub fn new(max_memory: usize) -> Self { + Self { + max_memory, + memory: 0, + table_elements: 0, + } + } +} + +/// Accounts a growth from `current` to `desired` against `used` of `max`. +/// A growth past the memory's or table's own `maximum` fails anyway, so it +/// is refused without being counted. +fn grow( + used: &mut usize, + max: usize, + current: usize, + desired: usize, + maximum: Option, +) -> bool { + if maximum.is_some_and(|m| desired > m) { + return false; + } + let added = desired.saturating_sub(current); + match used.checked_add(added) { + Some(total) if total <= max => { + *used = total; + true + } + _ => false, + } +} + +impl wasmtime::ResourceLimiter for Limits { + fn memory_growing( + &mut self, + current: usize, + desired: usize, + maximum: Option, + ) -> anyhow::Result { + Ok(grow( + &mut self.memory, + self.max_memory, + current, + desired, + maximum, + )) + } + + fn table_growing( + &mut self, + current: usize, + desired: usize, + maximum: Option, + ) -> anyhow::Result { + Ok(grow( + &mut self.table_elements, + MAX_TABLE_ELEMENTS, + current, + desired, + maximum, + )) + } + + fn instances(&self) -> usize { + MAX_INSTANCES + } + + fn memories(&self) -> usize { + MAX_MEMORIES + } + + fn tables(&self) -> usize { + MAX_TABLES + } +} + +#[cfg(test)] +mod tests { + use super::*; + use wasmtime::ResourceLimiter; + + #[test] + fn the_limit_covers_all_memories_together() { + let mut limits = Limits::new(100); + assert!(limits.memory_growing(0, 60, None).unwrap()); + // A second memory gets only what the first left. + assert!(!limits.memory_growing(0, 60, None).unwrap()); + assert!(limits.memory_growing(0, 40, None).unwrap()); + // Growing the first one further no longer fits. + assert!(!limits.memory_growing(60, 61, None).unwrap()); + assert_eq!(limits.memory, 100); + } + + #[test] + fn a_refused_growth_is_not_counted() { + let mut limits = Limits::new(100); + assert!(!limits.memory_growing(0, 101, None).unwrap()); + assert!(limits.memory_growing(0, 100, None).unwrap()); + assert!(!limits.memory_growing(0, usize::MAX, None).unwrap()); + // Past a memory's own maximum: refused and not counted. + let mut limits = Limits::new(100); + assert!(!limits.memory_growing(0, 50, Some(40)).unwrap()); + assert!(limits.memory_growing(0, 100, None).unwrap()); + } +} diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index 3453a9f..7774169 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -3,6 +3,13 @@ use std::path::PathBuf; use anyhow::Context; mod grants; +#[cfg(feature = "wasmtime-runtime")] +mod ipc; +#[cfg(feature = "wasmtime-runtime")] +mod limits; + +/// The default limit on a component's linear memory, in MiB. +const DEFAULT_MAX_MEMORY_MIB: usize = 256; use grants::{Grants, Preopen}; @@ -18,7 +25,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 +38,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 +58,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 +92,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 +120,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 +133,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 +146,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: limits::Limits, } impl IoView for State { @@ -199,7 +174,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 +198,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 +294,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}"); @@ -339,8 +317,10 @@ fn run_module( State { ctx, table: ResourceTable::new(), + limits: limits::Limits::new(max_memory), }, ); + store.limiter(|state| &mut state.limits); let command = Command::instantiate(&mut store, &component, &linker).context("instantiate component")?; @@ -635,6 +615,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 6443e80..593456e 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 2a7e14a..06291f9 100644 --- a/docs/security.md +++ b/docs/security.md @@ -14,7 +14,7 @@ The approval is recorded per user, in `$XDG_DATA_HOME/weft/approvals/`; weft ## 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=512` (threads included; the runtime runs a few per CPU). Independently of systemd, the runtime limits the component's linear memory to 256 MiB in total, across all its memories (`--max-memory-mib` changes it), and bounds its tables, memories and core instances: a memory or table that would grow past its 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, a longer line from weft-appd, or a failed send, or a write that blocks for 10 seconds, ends the connection for both sides, so weft-appd ends the session as it does when the component closes the connection. 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 07c232f..8854e6c 100644 --- a/tests/runtime/check_grants.py +++ b/tests/runtime/check_grants.py @@ -21,7 +21,10 @@ scenario passes a different set of `--grant` and `--preopen` arguments: are refused; - a wildcard fetch grant, and a grant for the name `localhost`: the local server is refused, since only an address named in a grant reaches this - machine or a local network; the connection is never made. + machine or a local network; the connection is never made; +- 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 @@ -164,11 +167,19 @@ def check(runner, data): if REQUESTS: raise AssertionError(f"refused fetches reached the server: {REQUESTS}") + _, 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 "