mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
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.
This commit is contained in:
parent
5c82b540e6
commit
044634692f
8 changed files with 251 additions and 66 deletions
|
|
@ -163,6 +163,8 @@ const SYSTEMD_SCOPE_ARGS: &[&str] = &[
|
|||
"CPUQuota=200%",
|
||||
"-p",
|
||||
"MemoryMax=512M",
|
||||
"-p",
|
||||
"TasksMax=64",
|
||||
];
|
||||
|
||||
fn systemd_cgroup_available() -> bool {
|
||||
|
|
|
|||
175
crates/weft-runtime/src/ipc.rs
Normal file
175
crates/weft-runtime/src/ipc.rs
Normal file
|
|
@ -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<u8>,
|
||||
/// Why the connection ended, once it has.
|
||||
closed: Option<&'static str>,
|
||||
}
|
||||
|
||||
impl IpcState {
|
||||
pub fn connect(path: &str) -> Option<Self> {
|
||||
let socket = UnixStream::connect(path).ok()?;
|
||||
Self::new(socket)
|
||||
}
|
||||
|
||||
fn new(socket: UnixStream) -> Option<Self> {
|
||||
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<String> {
|
||||
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<String> {
|
||||
loop {
|
||||
let pos = self.recv_buf.iter().position(|&b| b == b'\n')?;
|
||||
let raw: Vec<u8> = 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}");
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <app_id> <session_id> --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<String> = None;
|
||||
let mut module: Option<PathBuf> = 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::<usize>()
|
||||
.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<u8>,
|
||||
}
|
||||
|
||||
impl IpcState {
|
||||
fn connect(path: &str) -> Option<Self> {
|
||||
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<String> {
|
||||
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<u8> = 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<State> = Linker::new(&engine);
|
||||
add_to_linker_sync(&mut linker).context("add WASI to linker")?;
|
||||
let ipc_state: Arc<Mutex<Option<IpcState>>> = Arc::new(Mutex::new(None));
|
||||
let ipc_state: Arc<Mutex<Option<ipc::IpcState>>> = 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"));
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ systemd
|
|||
├── weft-servo-shell (user, after compositor)
|
||||
└── weft-appd (user, after compositor + servo-shell)
|
||||
└── per-session:
|
||||
├── weft-runtime <app_id> <session_id> --ipc-socket <path> --module <path> [--preopen HOST::GUEST::ro|rw]... [--grant CAP]...
|
||||
├── weft-runtime <app_id> <session_id> --ipc-socket <path> --module <path> [--preopen HOST::GUEST::ro|rw]... [--grant CAP]... [--max-memory-mib N]
|
||||
├── weft-app-shell <app_id> <session_id> --ui <path>
|
||||
└── weft-file-portal <socket> [--allow ...] [--allow-read ...]
|
||||
```
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
//! Test component that calls every grant-controlled operation once and
|
||||
//! prints one `PROBE <name> ok <detail>` or `PROBE <name> err <message>`
|
||||
//! 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 <name> ok <detail>` or `PROBE <name> err <message>` line for each.
|
||||
//!
|
||||
//! Fetch targets are read from `/probe/targets.txt` (`<name> <url>` 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<T: std::fmt::Debug, E: std::fmt::Display>(name: &str, result: Result<T, E>) {
|
||||
match result {
|
||||
|
|
@ -49,6 +52,14 @@ fn main() {
|
|||
);
|
||||
}
|
||||
}
|
||||
let mut big: Vec<u8> = 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),
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 "
|
||||
|
|
|
|||
Loading…
Reference in a new issue