diff --git a/crates/weft-runtime/src/ipc.rs b/crates/weft-runtime/src/ipc.rs index 687244b..998ac4e 100644 --- a/crates/weft-runtime/src/ipc.rs +++ b/crates/weft-runtime/src/ipc.rs @@ -11,8 +11,8 @@ 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. +/// 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. @@ -108,9 +108,12 @@ impl IpcState { 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) { @@ -179,6 +182,9 @@ mod tests { } 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] diff --git a/crates/weft-runtime/src/limits.rs b/crates/weft-runtime/src/limits.rs index 853082a..ab50324 100644 --- a/crates/weft-runtime/src/limits.rs +++ b/crates/weft-runtime/src/limits.rs @@ -30,7 +30,18 @@ impl Limits { } /// Accounts a growth from `current` to `desired` against `used` of `max`. -fn grow(used: &mut usize, max: usize, current: usize, desired: usize) -> bool { +/// 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 => { @@ -46,22 +57,29 @@ impl wasmtime::ResourceLimiter for Limits { &mut self, current: usize, desired: usize, - _maximum: Option, + maximum: Option, ) -> anyhow::Result { - Ok(grow(&mut self.memory, self.max_memory, current, desired)) + Ok(grow( + &mut self.memory, + self.max_memory, + current, + desired, + maximum, + )) } fn table_growing( &mut self, current: usize, desired: usize, - _maximum: Option, + maximum: Option, ) -> anyhow::Result { Ok(grow( &mut self.table_elements, MAX_TABLE_ELEMENTS, current, desired, + maximum, )) } @@ -101,5 +119,9 @@ mod tests { 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/docs/security.md b/docs/security.md index 6a7a379..8b8bcf3 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=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. +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