vetto 0.2.13

Daemon-less sandbox + security layer for AI coding agents (Landlock/Seatbelt, TUI statusline, post-session audit reports)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! Fail-closed multi-agent launcher.
//!
//! Every `MultiSession` owns its own `Backend`, `SandboxHandle`, event bus,
//! stats collector and captured stdout/stderr buffers. There is no shared
//! child process or unsandboxed fallback. All backend detection and policy
//! loading happens before any process is spawned; a spawn failure tears down
//! already-created handles and returns an error to the caller.
//!
//! Phase 4 (Step 23 & 24): Virtual port allocation, debug port guardrails,
//! sub-reaper configuration, and cross-agent isolation tracking.

#[cfg(unix)]
use std::path::Path;
use std::path::PathBuf;
#[cfg(unix)]
use std::sync::atomic::Ordering;
use std::sync::{atomic::AtomicBool, Arc, Mutex};
use std::time::Instant;

#[cfg(unix)]
use crate::config::NetMode;
#[cfg(unix)]
use crate::events::Event;
use crate::events::EventBus;
use crate::multi::isolation::IsolationBarrier;
use crate::multi::{AgentSpec, Manifest, MultiAggregator, MultiEventStream, VirtualPortPool};
#[cfg(unix)]
use crate::policy;
use crate::report::stats::StatsCollector;
use crate::report::{self, storage::ReportStorage, ReportOptions};
use crate::sandbox::SandboxHandle;
#[cfg(unix)]
use crate::sandbox::{Backend, SpawnOptions, StdioMode};
use anyhow::{bail, Context, Result};

#[cfg(unix)]
use std::collections::HashMap;
#[cfg(unix)]
use std::io::Read;
#[cfg(target_os = "linux")]
use std::os::fd::IntoRawFd;
#[cfg(unix)]
use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};

#[cfg(unix)]
const OUTPUT_CAP: usize = 512 * 1024;

#[derive(Default)]
pub struct OutputBuffers {
    pub stdout: Vec<u8>,
    pub stderr: Vec<u8>,
}

impl OutputBuffers {
    pub fn text(&self) -> String {
        let mut bytes = self.stdout.clone();
        bytes.extend_from_slice(&self.stderr);
        String::from_utf8_lossy(&bytes).into_owned()
    }
}

pub struct MultiSession {
    pub spec: AgentSpec,
    pub bus: EventBus,
    pub stats: StatsCollector,
    pub output: Arc<Mutex<OutputBuffers>>,
    pub handle: Arc<Mutex<SandboxHandle>>,
    pub finished: Arc<AtomicBool>,
    pub started: Instant,
    pub allocated_ports: Vec<u16>,
}

#[cfg(unix)]
struct PendingSession {
    spec: AgentSpec,
    net: NetMode,
    tier: policy::Tier,
    policy: policy::Policy,
    bus: EventBus,
    handle: SandboxHandle,
    stdout_r: OwnedFd,
    stderr_r: OwnedFd,
    broker_ctrl_fd: Option<OwnedFd>,
    notif_listener: Option<OwnedFd>,
    allocated_ports: Vec<u16>,
}

#[cfg(unix)]
impl PendingSession {
    fn terminate(&mut self) {
        self.handle.terminate();
    }
}

impl MultiSession {
    pub fn pause(&self) {
        if let Ok(mut handle) = self.handle.lock() {
            handle.pause();
        }
    }

    pub fn resume(&self) {
        if let Ok(mut handle) = self.handle.lock() {
            handle.resume();
        }
    }

    pub fn terminate(&self) {
        if let Ok(mut handle) = self.handle.lock() {
            handle.terminate();
        }
    }

    pub fn try_wait(&self) -> Option<i32> {
        self.handle.lock().ok()?.try_wait()
    }

    pub fn output_text(&self) -> String {
        self.output
            .lock()
            .map(|output| output.text())
            .unwrap_or_default()
    }
}

pub struct MultiRuntime {
    pub manifest: Manifest,
    pub sessions: Vec<MultiSession>,
    pub stream: MultiEventStream,
    pub aggregator: MultiAggregator,
    pub port_pool: VirtualPortPool,
    pub isolation_barrier: IsolationBarrier,
    pub report_dir: Option<PathBuf>,
}

impl MultiRuntime {
    /// Prepare and launch all agents. The preflight phase deliberately owns
    /// no child handles: invalid policy/network/command input is rejected
    /// before the first fork. Once spawning begins, any failure terminates
    /// every already-created sandbox before returning the error.
    #[cfg(unix)]
    pub fn launch(manifest: Manifest, project: PathBuf, home: PathBuf) -> Result<Self> {
        manifest.validate()?;

        // Configure supervisor process as sub-reaper so orphaned child processes
        // inside agent PID namespaces are adopted and reaped by Vetto.
        let _ = crate::multi::isolation::set_subreaper();

        let port_pool = VirtualPortPool::default();
        let isolation_barrier = IsolationBarrier::new();

        let mut prepared = Vec::with_capacity(manifest.agents.len());
        for (idx, spec) in manifest.agents.iter().enumerate() {
            let net = crate::config::parse_net_mode(&spec.net)
                .with_context(|| format!("agent '{}' network mode", spec.name))?;
            let backend = Backend::detect(net.clone(), spec.observe_seccomp)
                .with_context(|| format!("establish sandbox backend for agent '{}'", spec.name))?;
            let tier = backend.tier().unwrap_or(policy::Tier::Full);
            let policy =
                policy::loader::load(&spec.profile, spec.policy.as_deref(), &project, &home, tier)
                    .with_context(|| format!("load policy for agent '{}'", spec.name))?;
            let mut command = spec.command.clone();
            command[0] = resolve_in_path(&command[0])
                .with_context(|| format!("resolve command for agent '{}'", spec.name))?;
            // Do not silently permit a policy to exclude the executable.
            if !policy.in_read_scope(Path::new(&command[0])) {
                tracing::warn!(
                    agent = %spec.name,
                    command = %command[0],
                    "agent executable is outside policy read scope; sandbox exec may be denied"
                );
            }

            let allocated_ports = port_pool
                .allocate_ports(&spec.name, 4)
                .unwrap_or_else(|_| vec![port_pool.allocate_relay_port(idx)]);

            prepared.push(Prepared {
                spec: spec.clone(),
                net,
                backend: Some(backend),
                policy,
                command,
                tier,
                allocated_ports,
            });
        }

        // Single-threaded fork phase.
        let mut pending = Vec::with_capacity(prepared.len());
        for prepared in prepared {
            match spawn_one(prepared, &project) {
                Ok(session) => pending.push(session),
                Err(error) => {
                    for session in &mut pending {
                        session.terminate();
                    }
                    bail!("multi-agent launch aborted; no unsandboxed fallback: {error:#}");
                }
            }
        }

        let stream = MultiEventStream::new();
        let aggregator =
            MultiAggregator::new(manifest.agents.iter().map(|agent| agent.name.clone()));
        crate::multi::spawn_aggregator(&stream, aggregator.clone());
        let mut sessions = Vec::with_capacity(pending.len());
        for pending in pending {
            let session = activate_pending(pending, &project, &stream, &isolation_barrier);
            sessions.push(session);
        }

        Ok(Self {
            report_dir: manifest.report_dir.clone(),
            manifest,
            sessions,
            stream,
            aggregator,
            port_pool,
            isolation_barrier,
        })
    }

    #[cfg(not(unix))]
    pub fn launch(_manifest: Manifest, _project: PathBuf, _home: PathBuf) -> Result<Self> {
        bail!("multi-agent mode is unavailable on this platform; refusing to run unsandboxed")
    }

    pub fn terminate(&self, index: usize) -> Result<()> {
        let session = self
            .sessions
            .get(index)
            .ok_or_else(|| anyhow::anyhow!("unknown multi-agent pane {index}"))?;
        session.terminate();
        Ok(())
    }

    pub fn terminate_all(&self) {
        for session in &self.sessions {
            session.terminate();
        }
    }

    pub fn combined_report(&self) -> serde_json::Value {
        self.aggregator.report_json()
    }

    pub fn write_reports(&self) -> Result<Vec<PathBuf>> {
        let mut written = Vec::new();
        let rows = self.aggregator.snapshot();
        for agent in &self.manifest.agents {
            let dir = agent.report_path(self.report_dir.as_deref());
            let options = ReportOptions {
                report_dir: Some(dir),
                auto_cleanup: false,
                retention: None,
                max_age_secs: None,
            };
            let storage = ReportStorage::new(&options)
                .with_context(|| format!("prepare report directory for agent '{}'", agent.name))?;
            let row = rows
                .iter()
                .find(|stats| stats.name == agent.name)
                .cloned()
                .unwrap_or_else(|| crate::multi::AgentStats::new(agent.name.clone()));
            let mut value = serde_json::to_value(row).context("serialize agent report")?;
            report::sanitize_json_strings(&mut value);
            let text = serde_json::to_string_pretty(&value).context("render agent report")?;
            let path = storage
                .write("json", &text)
                .with_context(|| format!("write report for agent '{}'", agent.name))?;
            written.push(path);
        }
        let combined_dir = self
            .report_dir
            .clone()
            .unwrap_or_else(|| PathBuf::from("."));
        let options = ReportOptions {
            report_dir: Some(combined_dir),
            auto_cleanup: false,
            retention: None,
            max_age_secs: None,
        };
        let storage = ReportStorage::new(&options).context("prepare combined report directory")?;
        let mut combined = self.combined_report();
        report::sanitize_json_strings(&mut combined);
        let combined = serde_json::to_string_pretty(&combined).context("render combined report")?;
        let combined_path = storage
            .write("json", &combined)
            .context("write combined report")?;
        written.push(combined_path);
        Ok(written)
    }
}

#[cfg(unix)]
struct Prepared {
    spec: AgentSpec,
    net: NetMode,
    backend: Option<Backend>,
    policy: policy::Policy,
    command: Vec<String>,
    tier: policy::Tier,
    allocated_ports: Vec<u16>,
}

#[cfg(unix)]
fn spawn_one(prepared: Prepared, project: &Path) -> Result<PendingSession> {
    let Prepared {
        spec,
        net,
        backend,
        policy,
        command,
        tier,
        allocated_ports,
    } = prepared;
    let backend = backend.ok_or_else(|| anyhow::anyhow!("sandbox backend was consumed"))?;
    let (stdout_r, stdout_w) = pipe2()?;
    let (stderr_r, stderr_w) = pipe2()?;
    let options = SpawnOptions {
        agent_cmd: command,
        cwd: project.to_path_buf(),
        env_extra: relay_env(&net),
        stdio: StdioMode::Captured {
            stdout_w: stdout_w.as_raw_fd(),
            stderr_w: stderr_w.as_raw_fd(),
        },
    };
    let spawned = backend
        .spawn(&policy, options)
        .with_context(|| format!("spawn agent '{}' inside its sandbox", spec.name))?;
    let crate::sandbox::Spawned {
        handle,
        broker_ctrl_fd,
        relay_port: _relay_port,
        notif_listener,
    } = spawned;

    drop(stdout_w);
    drop(stderr_w);

    Ok(PendingSession {
        spec,
        net,
        tier,
        policy,
        bus: EventBus::new(),
        handle,
        stdout_r,
        stderr_r,
        broker_ctrl_fd,
        notif_listener,
        allocated_ports,
    })
}

#[cfg(unix)]
fn activate_pending(
    pending: PendingSession,
    project: &Path,
    stream: &MultiEventStream,
    isolation_barrier: &IsolationBarrier,
) -> MultiSession {
    #[cfg(not(target_os = "linux"))]
    let _ = project;
    let PendingSession {
        spec,
        net,
        tier,
        policy,
        bus,
        handle,
        stdout_r,
        stderr_r,
        broker_ctrl_fd,
        notif_listener,
        allocated_ports,
    } = pending;
    let stats = StatsCollector::spawn(&bus);
    let root_pid = handle.root_pid;

    // Register agent in the isolation barrier
    let is_full = tier == policy::Tier::Full;
    isolation_barrier.register_agent(
        &spec.name,
        root_pid,
        is_full,
        is_full,
        policy.limits.address_space_bytes,
    );

    // Subscribe the aggregate bridge before publishing SessionStarted
    stream.bridge_agent(spec.name.clone(), &bus);
    bus.publish(Event::SessionStarted {
        ts: crate::events::types::now(),
        pid: root_pid,
        tier: tier.label().to_string(),
        net_mode: net.label(),
        profile: policy.name.clone(),
    });

    #[cfg(target_os = "linux")]
    {
        if let Some(fd) = broker_ctrl_fd {
            let broker_policy = match &net {
                NetMode::Allowlist(domains) => {
                    crate::sandbox::linux::net_relay::BrokerPolicy::Allowlist(domains.clone())
                }
                NetMode::Strict(rules) => {
                    crate::sandbox::linux::net_relay::BrokerPolicy::Strict(rules.clone())
                }
                NetMode::Ask => crate::sandbox::linux::net_relay::BrokerPolicy::Ask,
                NetMode::Off => {
                    crate::sandbox::linux::net_relay::BrokerPolicy::Allowlist(Vec::new())
                }
            };
            let debug_config = spec
                .debug_ports
                .as_ref()
                .map(|p| crate::sandbox::linux::debug_guard::DebugPortConfig {
                    isolate_devtools: p.isolate_devtools,
                    isolate_node_inspect: p.isolate_node_inspect,
                    isolate_debugpy: p.isolate_debugpy,
                    allowed_ports: p.allowed_ports.clone(),
                })
                .unwrap_or_default();
            let debug_guard = crate::sandbox::linux::debug_guard::DebugPortGuard::new(debug_config);
            let broker_config = crate::sandbox::linux::net_relay::BrokerConfig {
                policy: broker_policy,
                debug_guard: Some(debug_guard),
                mode: crate::sandbox::linux::net_relay::RelayMode::NetNs,
                allow_cidr: policy.allow_cidr.clone(),
                quotas: policy.net_quota.clone(),
            };
            crate::sandbox::linux::net_relay::spawn_broker(
                fd.into_raw_fd(),
                broker_config,
                bus.clone(),
            );
        }
        if let Some(fd) = notif_listener {
            crate::sandbox::linux::observe_seccomp::spawn_notifier(
                fd,
                bus.clone(),
                Arc::new(policy.clone()),
                project.to_path_buf(),
            );
        }
        crate::sandbox::linux::visibility::spawn_poller(bus.clone(), vec![root_pid]);
    }
    #[cfg(all(unix, not(target_os = "linux")))]
    {
        let _ = (broker_ctrl_fd, notif_listener);
    }

    let output = Arc::new(Mutex::new(OutputBuffers::default()));
    spawn_pipe_reader(stdout_r, Arc::clone(&output), true);
    spawn_pipe_reader(stderr_r, Arc::clone(&output), false);

    let handle = Arc::new(Mutex::new(handle));
    let finished = Arc::new(AtomicBool::new(false));
    let wait_handle = Arc::clone(&handle);
    let wait_finished = Arc::clone(&finished);
    let wait_bus = bus.clone();
    let agent_name = spec.name.clone();
    let barrier_clone = isolation_barrier.clone();

    std::thread::Builder::new()
        .name(format!("vetto-multi-wait-{}", spec.name))
        .spawn(move || {
            let code = wait_handle
                .lock()
                .map(|mut handle| handle.wait())
                .unwrap_or(-1);
            wait_bus.publish(Event::SessionEnded {
                ts: crate::events::types::now(),
                exit_code: code,
                duration_secs: 0,
            });
            barrier_clone.unregister_agent(&agent_name);
            wait_finished.store(true, Ordering::SeqCst);
        })
        .expect("spawn multi wait thread");

    MultiSession {
        spec,
        bus,
        stats,
        output,
        handle,
        finished,
        started: Instant::now(),
        allocated_ports,
    }
}

#[cfg(unix)]
fn spawn_pipe_reader(fd: OwnedFd, output: Arc<Mutex<OutputBuffers>>, stdout: bool) {
    std::thread::Builder::new()
        .name("vetto-multi-output".into())
        .spawn(move || {
            let mut file: std::fs::File = fd.into();
            let mut chunk = [0u8; 8192];
            loop {
                match file.read(&mut chunk) {
                    Ok(0) | Err(_) => break,
                    Ok(n) => {
                        if let Ok(mut output) = output.lock() {
                            let target = if stdout {
                                &mut output.stdout
                            } else {
                                &mut output.stderr
                            };
                            target.extend_from_slice(&chunk[..n]);
                            if target.len() > OUTPUT_CAP {
                                let excess = target.len() - OUTPUT_CAP;
                                target.drain(..excess);
                            }
                        }
                    }
                }
            }
        })
        .expect("spawn multi output reader");
}

#[cfg(unix)]
fn pipe2() -> Result<(OwnedFd, OwnedFd)> {
    let mut fds = [0 as libc::c_int; 2];
    // SAFETY: valid out-array for the libc pipe call.
    if unsafe { libc::pipe(fds.as_mut_ptr()) } != 0 {
        bail!("pipe: {}", std::io::Error::last_os_error());
    }
    for fd in fds {
        // SAFETY: fd came from the successful pipe call.
        let flags = unsafe { libc::fcntl(fd, libc::F_GETFD) };
        if flags < 0 {
            let error = std::io::Error::last_os_error();
            // SAFETY: both descriptors came from the successful pipe call.
            unsafe {
                libc::close(fds[0]);
                libc::close(fds[1]);
            }
            bail!("fcntl(F_GETFD): {error}");
        }
        // SAFETY: fd came from the successful pipe call.
        if unsafe { libc::fcntl(fd, libc::F_SETFD, flags | libc::FD_CLOEXEC) } < 0 {
            let error = std::io::Error::last_os_error();
            // SAFETY: both descriptors came from the successful pipe call.
            unsafe {
                libc::close(fds[0]);
                libc::close(fds[1]);
            }
            bail!("fcntl(F_SETFD): {error}");
        }
    }
    // SAFETY: fresh descriptors from a successful pipe and CLOEXEC setup.
    Ok((unsafe { OwnedFd::from_raw_fd(fds[0]) }, unsafe {
        OwnedFd::from_raw_fd(fds[1])
    }))
}

#[cfg(target_os = "linux")]
fn relay_env(net: &NetMode) -> HashMap<String, String> {
    let mut env = HashMap::new();
    if net.uses_relay() {
        for (key, value) in crate::sandbox::linux::net_relay::build_proxy_env(
            crate::sandbox::linux::net_relay::RELAY_PORT_BASE,
        ) {
            env.insert(key, value);
        }
    }
    env
}

#[cfg(all(unix, not(target_os = "linux")))]
fn relay_env(_net: &NetMode) -> HashMap<String, String> {
    HashMap::new()
}

#[cfg(unix)]
fn resolve_in_path(command: &str) -> Result<String> {
    if command.contains('/') {
        return Ok(command.to_string());
    }
    for dir in std::env::var_os("PATH")
        .unwrap_or_default()
        .to_string_lossy()
        .split(':')
    {
        if dir.is_empty() {
            continue;
        }
        let candidate = Path::new(dir).join(command);
        if std::fs::metadata(&candidate)
            .map(|meta| meta.is_file())
            .unwrap_or(false)
        {
            return Ok(candidate.to_string_lossy().into_owned());
        }
    }
    bail!("agent command '{command}' not found in PATH")
}

#[cfg(test)]
mod tests {
    #[cfg(unix)]
    use super::*;
    use crate::multi::parse_manifest_str;
    use std::path::Path;

    #[test]
    fn report_directory_is_per_agent() {
        let manifest = parse_manifest_str(
            r#"
                [[agents]]
                name = "one"
                command = ["one"]
                [[agents]]
                name = "two"
                command = ["two"]
            "#,
        )
        .expect("manifest");
        let root = Path::new("reports");
        assert_ne!(
            manifest.agents[0].report_path(Some(root)),
            manifest.agents[1].report_path(Some(root))
        );
    }

    #[cfg(unix)]
    #[test]
    fn write_reports_refuses_symlinked_agent_directory() {
        use std::os::unix::fs::symlink;
        use std::time::{SystemTime, UNIX_EPOCH};

        let nonce = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("clock after epoch")
            .as_nanos();
        let root = std::env::temp_dir().join(format!("vetto-multi-storage-{nonce}"));
        let real = root.join("real");
        let link = root.join("link");
        std::fs::create_dir_all(&real).expect("create real report directory");
        symlink(&real, &link).expect("create report directory symlink");

        let manifest = Manifest {
            version: 1,
            agents: vec![AgentSpec {
                name: "one".into(),
                command: vec!["agent".into()],
                profile: "default".into(),
                policy: None,
                net: "off".into(),
                observe_seccomp: false,
                report_dir: Some(link.clone()),
                debug_ports: None,
            }],
            report_dir: Some(root.join("combined")),
        };
        let runtime = MultiRuntime {
            manifest,
            sessions: Vec::new(),
            stream: MultiEventStream::new(),
            aggregator: MultiAggregator::new(["one".to_string()]),
            port_pool: VirtualPortPool::default(),
            isolation_barrier: IsolationBarrier::new(),
            report_dir: Some(root.join("combined")),
        };

        assert!(runtime.write_reports().is_err());
        assert!(real
            .read_dir()
            .expect("read real directory")
            .next()
            .is_none());

        std::fs::remove_file(&link).expect("remove report directory symlink");
        std::fs::remove_dir(&real).expect("remove real report directory");
        std::fs::remove_dir(&root).expect("remove report root");
    }
}