boxwren 0.2.3

Local-first WebAssembly sandbox for untrusted agent code. Deny-by-default limits, capability grants, and a kill switch.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use wasmtime::{Instance, Module, Store, UpdateDeadline, Val, ValType};

use crate::caps::Grants;
use crate::capture::Capture;
use crate::engine::{Engine, KillHandle};
use crate::error::Error;
use crate::limiter::RunLimiter;
use crate::limits::Limits;
use crate::report::{Event, Report, SCHEMA_VERSION, Status};

/// What to invoke after the module is instantiated.
#[derive(Clone, Debug)]
pub enum Entry {
    /// Call an export with no parameters. An `i32` or `i64` result is recorded.
    Export(String),
    /// Call WASI `_start` as a command. Arguments are the guest's argv.
    Command { args: Vec<String> },
}

/// One execution of untrusted code.
#[derive(Clone, Debug)]
pub struct RunRequest {
    pub wasm: Vec<u8>,
    pub entry: Entry,
    pub limits: Limits,
    pub grants: Grants,
    pub args: Vec<String>,
}

pub(crate) struct RunControl {
    pub limiter: RunLimiter,
    pub killed: Arc<AtomicBool>,
    /// Wall clock starts at guest entry (`begin_guest_wall`), not during compile/instantiate.
    pub started: Option<Instant>,
    pub wall: Duration,
    pub timed_out: bool,
    pub journal: Arc<Mutex<Vec<Event>>>,
    pub stdout: Capture,
    pub stderr: Capture,
}

impl RunControl {
    pub(crate) fn new(limits: &Limits, killed: Arc<AtomicBool>) -> Self {
        Self {
            limiter: RunLimiter::new(limits.memory_bytes),
            killed,
            started: None,
            wall: limits.wall_time,
            timed_out: false,
            journal: Arc::new(Mutex::new(Vec::new())),
            stdout: Capture::new(limits.max_stdio_bytes),
            stderr: Capture::new(limits.max_stdio_bytes),
        }
    }
}

pub(crate) struct StoreState {
    pub control: RunControl,
}

impl Engine {
    /// Run a module. Timeout still applies. Nothing is killed unless the
    /// process is torn down.
    pub fn execute(&self, request: RunRequest) -> Result<Report, Error> {
        let kill = self.kill_handle();
        self.execute_with(request, &kill)
    }

    /// Run a module. [`KillHandle::kill`] from another thread stops this run
    /// and leaves other runs on this engine alone.
    pub fn execute_with(&self, request: RunRequest, kill: &KillHandle) -> Result<Report, Error> {
        self.execute_with_preflight(request, kill, &AllowAll)
    }

    /// Like [`Self::execute_with`], but asks `preflight` before instantiation.
    /// Keywarden (or any policy) can deny the run without starting the guest.
    pub fn execute_with_preflight(
        &self,
        request: RunRequest,
        kill: &KillHandle,
        preflight: &dyn crate::hooks::Preflight,
    ) -> Result<Report, Error> {
        if !self.same_engine(kill) {
            return Err(Error::ForeignKillHandle);
        }
        preflight.allow(&request)?;
        let warnings = request.grants.validate()?;
        if request.wasm.len() > request.limits.max_module_bytes {
            return Ok(rejected(
                warnings,
                format!(
                    "module is {} bytes, over the {} byte cap",
                    request.wasm.len(),
                    request.limits.max_module_bytes
                ),
            ));
        }

        if crate::wasi::is_component(&request.wasm) {
            return crate::wasi::execute_component(self, request, kill, warnings);
        }

        let module = match self.module(&request.wasm) {
            Ok(module) => module,
            Err(err) => return Ok(rejected(warnings, err.to_string())),
        };

        if needs_wasi(&module, &request.entry) {
            return crate::wasi::execute_preview1(self, module, request, kill, warnings);
        }

        execute_raw(self, module, request, kill, warnings)
    }
}

struct AllowAll;

impl crate::hooks::Preflight for AllowAll {
    fn allow(&self, _request: &RunRequest) -> Result<(), crate::hooks::PolicyDeny> {
        Ok(())
    }
}

fn needs_wasi(module: &Module, entry: &Entry) -> bool {
    if matches!(entry, Entry::Command { .. }) {
        return true;
    }
    module.imports().any(|import| {
        let module = import.module();
        module.starts_with("wasi_") || module.starts_with("wasi:")
    })
}

fn execute_raw(
    engine: &Engine,
    module: Module,
    request: RunRequest,
    kill: &KillHandle,
    warnings: Vec<String>,
) -> Result<Report, Error> {
    let Entry::Export(name) = &request.entry else {
        return Ok(rejected(
            warnings,
            "WASI command entry requires the WASI linker".into(),
        ));
    };

    let mut store = open_store(engine, &request, kill)?;

    let instance = match Instance::new(&mut store, &module, &[]) {
        Ok(instance) => instance,
        Err(err) => {
            return Ok(finish(
                &store,
                &request,
                warnings,
                Instant::now(),
                None,
                Err(err),
            ));
        }
    };
    let Some(func) = instance.get_func(&mut store, name) else {
        return Ok(rejected(
            warnings,
            format!("module does not export `{name}`"),
        ));
    };
    let ty = func.ty(&store);
    if ty.params().len() != 0 {
        return Ok(rejected(
            warnings,
            format!("export `{name}` takes parameters; boxwren calls exports with none"),
        ));
    }
    if ty.results().any(|kind| !is_number(&kind)) {
        return Ok(rejected(
            warnings,
            format!("export `{name}` returns a non-numeric value"),
        ));
    }
    let mut results = ty.results().map(zero_val).collect::<Vec<_>>();
    let started = begin_guest_wall(&mut store);
    let result = func.call(&mut store, &[], &mut results);
    Ok(finish(
        &store,
        &request,
        warnings,
        started,
        results.first(),
        result.map(|_| ()),
    ))
}

fn is_number(kind: &ValType) -> bool {
    matches!(
        kind,
        ValType::I32 | ValType::I64 | ValType::F32 | ValType::F64
    )
}

fn zero_val(kind: ValType) -> Val {
    match kind {
        ValType::I32 => Val::I32(0),
        ValType::I64 => Val::I64(0),
        ValType::F32 => Val::F32(0),
        ValType::F64 => Val::F64(0),
        _ => Val::I32(0),
    }
}

pub(crate) fn open_store(
    engine: &Engine,
    request: &RunRequest,
    kill: &KillHandle,
) -> Result<Store<StoreState>, Error> {
    let state = StoreState {
        control: RunControl::new(&request.limits, kill.flag()),
    };
    let mut store = Store::new(engine.wasm(), state);
    arm(&mut store, request.limits.fuel)?;
    Ok(store)
}

pub(crate) fn arm<T>(store: &mut Store<T>, fuel: u64) -> Result<(), Error>
where
    T: HasControl + Send + 'static,
{
    store.set_fuel(fuel)?;
    store.limiter(|state| &mut state.control_mut().limiter);
    // Wall clock stays unset until `begin_guest_wall` so Cranelift compile and
    // Wasm instantiate of large guests (e.g. QuickJS) do not burn the deadline.
    store.data_mut().control_mut().started = None;
    store.epoch_deadline_callback(|mut store| {
        let control = store.data_mut().control_mut();
        if control.killed.load(Ordering::Acquire) {
            return Ok(UpdateDeadline::Interrupt);
        }
        if control
            .started
            .is_some_and(|started| started.elapsed() >= control.wall)
        {
            control.timed_out = true;
            return Ok(UpdateDeadline::Interrupt);
        }
        Ok(UpdateDeadline::Continue(1))
    });
    store.set_epoch_deadline(1);
    Ok(())
}

/// Start measuring wall time for guest entry (after instantiate).
pub(crate) fn begin_guest_wall<T>(store: &mut Store<T>) -> Instant
where
    T: HasControl,
{
    let now = Instant::now();
    store.data_mut().control_mut().started = Some(now);
    now
}

pub(crate) trait HasControl {
    fn control_mut(&mut self) -> &mut RunControl;
}

impl HasControl for StoreState {
    fn control_mut(&mut self) -> &mut RunControl {
        &mut self.control
    }
}

pub(crate) fn finish(
    store: &Store<StoreState>,
    request: &RunRequest,
    warnings: Vec<String>,
    started: Instant,
    first_result: Option<&Val>,
    result: Result<(), wasmtime::Error>,
) -> Report {
    report_from(
        store.get_fuel().unwrap_or(request.limits.fuel),
        &store.data().control,
        request,
        warnings,
        started,
        first_result,
        result,
    )
}

pub(crate) fn report_from(
    fuel_remaining: u64,
    control: &RunControl,
    request: &RunRequest,
    warnings: Vec<String>,
    started: Instant,
    first_result: Option<&Val>,
    result: Result<(), wasmtime::Error>,
) -> Report {
    let fuel_used = request.limits.fuel.saturating_sub(fuel_remaining);
    let peak_memory = control.limiter.peak_memory as u64;
    let memory_denied = control.limiter.memory_denied;
    let killed = control.killed.load(Ordering::Acquire);
    let timed_out = control.timed_out;
    let duration_ms = u64::try_from(started.elapsed().as_millis()).unwrap_or(u64::MAX);
    let (stdout, stdout_truncated) = control.stdout.text();
    let (stderr, stderr_truncated) = control.stderr.text();
    let events = control
        .journal
        .lock()
        .unwrap_or_else(|poison| poison.into_inner())
        .clone();

    let (status, exit_code) = match result {
        Ok(()) if killed => (Status::Killed, None),
        Ok(()) if memory_denied => (Status::MemoryLimited, None),
        Ok(()) => {
            let code = matches!(request.entry, Entry::Command { .. }).then_some(0);
            (Status::Success, code)
        }
        Err(err) => classify(err, killed, timed_out, memory_denied),
    };

    Report {
        schema_version: SCHEMA_VERSION,
        status,
        exit_code,
        return_value: first_result.and_then(val_i64),
        duration_ms,
        fuel_used,
        peak_memory,
        stdout,
        stderr,
        stdout_truncated,
        stderr_truncated,
        events,
        warnings,
    }
}

fn classify(
    err: wasmtime::Error,
    killed: bool,
    timed_out: bool,
    memory_denied: bool,
) -> (Status, Option<i32>) {
    if killed {
        return (Status::Killed, None);
    }
    if timed_out {
        return (Status::TimedOut, None);
    }
    if let Some(trap) = err.downcast_ref::<wasmtime::Trap>()
        && matches!(trap, wasmtime::Trap::OutOfFuel)
    {
        return (Status::FuelExhausted, None);
    }
    if memory_denied || looks_like_memory_limit(&err) {
        return (Status::MemoryLimited, None);
    }
    if let Some(exit) = err.downcast_ref::<wasmtime_wasi::I32Exit>() {
        return (Status::Success, Some(exit.0));
    }
    if let Some(trap) = err.downcast_ref::<wasmtime::Trap>()
        && matches!(trap, wasmtime::Trap::Interrupt)
    {
        return (Status::TimedOut, None);
    }
    let message = trap_message(&err);
    (Status::Trapped { message }, None)
}

fn trap_message(err: &wasmtime::Error) -> String {
    let root = err.root_cause().to_string();
    let line = root.lines().next().unwrap_or(&root).trim();
    let line = if line.is_empty() || line.starts_with("error while executing") {
        err.to_string()
    } else {
        line.to_string()
    };
    const LIMIT: usize = 240;
    if line.len() <= LIMIT {
        line
    } else {
        line.chars().take(LIMIT).collect()
    }
}

fn looks_like_memory_limit(err: &wasmtime::Error) -> bool {
    let text = err.to_string().to_ascii_lowercase();
    text.contains("memory")
        && (text.contains("limit") || text.contains("exceed") || text.contains("maximum"))
}

fn val_i64(val: &Val) -> Option<i64> {
    match val {
        Val::I32(value) => Some(i64::from(*value)),
        Val::I64(value) => Some(*value),
        _ => None,
    }
}

pub(crate) fn rejected_public(warnings: Vec<String>, message: String) -> Report {
    rejected(warnings, message)
}

fn rejected(warnings: Vec<String>, message: String) -> Report {
    Report {
        schema_version: SCHEMA_VERSION,
        status: Status::Rejected { message },
        exit_code: None,
        return_value: None,
        duration_ms: 0,
        fuel_used: 0,
        peak_memory: 0,
        stdout: String::new(),
        stderr: String::new(),
        stdout_truncated: false,
        stderr_truncated: false,
        events: Vec::new(),
        warnings,
    }
}