From cbc984eef675e6e8213f728c530d3d7ef6ea5d3a Mon Sep 17 00:00:00 2001 From: Marco Allegretti Date: Fri, 9 Oct 2026 18:39:05 +0000 Subject: [PATCH] fix(runtime): limit a component's memory in total and end IPC on failed sends The memory limit applied to each linear memory separately, so a component made of several core modules could multiply it. A limiter now accounts all memories together, and bounds table elements, memories, tables and core instances. IPC fixes: - A failed or timed-out send (10 seconds) ends the connection, since part of the line may already have been written. - A line over the limit ends the connection, also when its newline arrives in the same read. - A line that is not UTF-8 is dropped and logged. TasksMax is raised to 512, since the runtime runs a few threads per CPU. Counter and Notes still run under the limits (check_counter.py and check_notes.py), and check_grants.py passes. --- crates/weft-appd/src/runtime.rs | 2 +- crates/weft-runtime/src/ipc.rs | 69 ++++++++++++++++---- crates/weft-runtime/src/limits.rs | 105 ++++++++++++++++++++++++++++++ crates/weft-runtime/src/main.rs | 12 ++-- docs/security.md | 2 +- 5 files changed, 168 insertions(+), 22 deletions(-) create mode 100644 crates/weft-runtime/src/limits.rs diff --git a/crates/weft-appd/src/runtime.rs b/crates/weft-appd/src/runtime.rs index 13c9ce2..fb8d1b5 100644 --- a/crates/weft-appd/src/runtime.rs +++ b/crates/weft-appd/src/runtime.rs @@ -164,7 +164,7 @@ const SYSTEMD_SCOPE_ARGS: &[&str] = &[ "-p", "MemoryMax=512M", "-p", - "TasksMax=64", + "TasksMax=512", ]; fn systemd_cgroup_available() -> bool { diff --git a/crates/weft-runtime/src/ipc.rs b/crates/weft-runtime/src/ipc.rs index 2900e56..687244b 100644 --- a/crates/weft-runtime/src/ipc.rs +++ b/crates/weft-runtime/src/ipc.rs @@ -11,6 +11,9 @@ use std::os::unix::net::UnixStream; /// The longest message in either direction, in bytes. pub const MAX_MESSAGE: usize = 64 * 1024; +/// How long sending one 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> { @@ -50,15 +53,23 @@ impl IpcState { 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 + .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. @@ -81,9 +92,8 @@ impl IpcState { 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(); + if self.recv_buf.len() > MAX_MESSAGE + 2 { + self.overlong(); break; } } @@ -103,8 +113,13 @@ impl IpcState { self.closed = Some(reason); } + 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, or longer than a message, is dropped. + /// 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')?; @@ -112,10 +127,12 @@ impl IpcState { 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; + self.overlong(); + return None; } - if let Ok(message) = String::from_utf8(text.to_vec()) { - return Some(message); + match String::from_utf8(text.to_vec()) { + Ok(message) => return Some(message), + Err(_) => tracing::warn!("IPC message that is not UTF-8 dropped"), } } } @@ -164,6 +181,34 @@ mod tests { assert!(ipc.send("after").is_err()); } + #[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(); diff --git a/crates/weft-runtime/src/limits.rs b/crates/weft-runtime/src/limits.rs new file mode 100644 index 0000000..853082a --- /dev/null +++ b/crates/weft-runtime/src/limits.rs @@ -0,0 +1,105 @@ +//! 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`. +fn grow(used: &mut usize, max: usize, current: usize, desired: usize) -> bool { + 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)) + } + + fn table_growing( + &mut self, + current: usize, + desired: usize, + _maximum: Option, + ) -> anyhow::Result { + Ok(grow( + &mut self.table_elements, + MAX_TABLE_ELEMENTS, + current, + desired, + )) + } + + 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()); + } +} diff --git a/crates/weft-runtime/src/main.rs b/crates/weft-runtime/src/main.rs index d3f589d..9c5b92d 100644 --- a/crates/weft-runtime/src/main.rs +++ b/crates/weft-runtime/src/main.rs @@ -5,6 +5,8 @@ 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; @@ -147,7 +149,7 @@ fn run_module( struct State { ctx: WasiCtx, table: ResourceTable, - limits: wasmtime::StoreLimits, + limits: limits::Limits, } impl IoView for State { @@ -319,13 +321,7 @@ 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(), + limits: limits::Limits::new(max_memory), }, ); store.limiter(|state| &mut state.limits); diff --git a/docs/security.md b/docs/security.md index 4a72e9a..6a7a379 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%`, `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. +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 or timed-out send (10 seconds), ends the connection, after which sending reports that it is closed. CPU time within the quota is not limited per component. ## Filesystem Isolation