vetto 0.2.16

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
//! Best-effort /proc visibility poller (ALLOWED operations only).
//!
//! Walks `/proc/<pid>/fd/` every 50ms while active, backing off to 500ms
//! after five seconds and 2s after thirty seconds for processes in the sandboxed
//! subtree and publishes FileObserved/ExecObserved events. Honest limits:
//! misses sub-100ms opens; fd flags come from `/proc/<pid>/fdinfo`; this is
//! observation, never enforcement.

use crate::events::{bus::EventBus, Event, FileAccess};
use std::collections::{HashMap, HashSet, VecDeque};
use std::time::{Duration, Instant};

pub const POLL_INTERVAL_MS: u64 = 50;
pub const ACTIVE_POLL_INTERVAL_MS: u64 = 50;
pub const IDLE_POLL_INTERVAL_MS: u64 = 500;
pub const LONG_IDLE_POLL_INTERVAL_MS: u64 = 2_000;
pub const IDLE_AFTER_MS: u64 = 5_000;
pub const LONG_IDLE_AFTER_MS: u64 = 30_000;
pub const MAX_PATH_CACHE_ENTRIES: usize = 10_000;
const MAX_EVENTS_PER_TICK: usize = 200;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct LifecycleToken {
    pub pid: u32,
    pub start_time: u64,
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct InvalidationToken {
    pub process: LifecycleToken,
    pub fd: i32,
}

#[derive(Debug, Clone)]
struct PathCacheEntry {
    path: String,
    generation: u64,
}

/// Bounded LRU cache for observed `/proc/<pid>/fd/<n>` targets.
///
/// The process start time is part of every key, so PID reuse cannot inherit a
/// prior process's path observations. Callers can explicitly invalidate a
/// descriptor or all descriptors for a process when a lifecycle token changes.
pub struct PathCache {
    capacity: usize,
    generation: u64,
    entries: HashMap<InvalidationToken, PathCacheEntry>,
    lru: VecDeque<(InvalidationToken, u64)>,
}

impl Default for PathCache {
    fn default() -> Self {
        Self::new(MAX_PATH_CACHE_ENTRIES)
    }
}

impl PathCache {
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity: capacity.clamp(1, MAX_PATH_CACHE_ENTRIES),
            generation: 0,
            entries: HashMap::new(),
            lru: VecDeque::new(),
        }
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Record a target and return true when it is new or changed.
    pub fn observe(&mut self, token: InvalidationToken, path: &str) -> bool {
        self.generation = self.generation.wrapping_add(1);
        if let Some(entry) = self.entries.get_mut(&token) {
            let changed = entry.path != path;
            entry.path.clear();
            entry.path.push_str(path);
            entry.generation = self.generation;
            self.lru.push_back((token, self.generation));
            self.compact_lru_if_needed();
            return changed;
        }
        self.entries.insert(
            token,
            PathCacheEntry {
                path: path.to_string(),
                generation: self.generation,
            },
        );
        self.lru.push_back((token, self.generation));
        self.evict_oldest();
        self.compact_lru_if_needed();
        true
    }

    pub fn invalidate(&mut self, token: InvalidationToken) {
        self.entries.remove(&token);
    }

    pub fn invalidate_process(&mut self, process: LifecycleToken) {
        self.entries.retain(|token, _| token.process != process);
    }

    pub fn clear(&mut self) {
        self.entries.clear();
        self.lru.clear();
    }

    fn compact_lru_if_needed(&mut self) {
        // Every refresh appends a generation marker. Compact stale markers so
        // the metadata queue cannot grow without bound during a long session,
        // even though the entry map itself is already capacity-bounded.
        let threshold = self.capacity.saturating_mul(2).max(64);
        if self.lru.len() <= threshold {
            return;
        }
        let mut live: Vec<_> = self
            .entries
            .iter()
            .map(|(token, entry)| (*token, entry.generation))
            .collect();
        live.sort_unstable_by_key(|(_, generation)| *generation);
        self.lru = live.into_iter().collect();
    }

    fn evict_oldest(&mut self) {
        while self.entries.len() > self.capacity {
            let Some((token, generation)) = self.lru.pop_front() else {
                self.entries.clear();
                return;
            };
            if self
                .entries
                .get(&token)
                .is_some_and(|entry| entry.generation == generation)
            {
                self.entries.remove(&token);
            }
        }
    }
}

/// One changed, user-visible descriptor found by [`scan_fds`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FdObservation {
    pub pid: u32,
    pub fd: i32,
    pub path: String,
    pub access: FileAccess,
    pub comm: String,
}

/// Scan one process's `/proc/<pid>/fd` directory and return newly observed or
/// changed user-visible descriptors.
///
/// This extracted primitive performs observation only. It does not publish
/// events, alter the process, or enforce policy. The caller owns the cache so
/// repeated scans can model the poller's normal steady-state cost.
pub fn scan_fds(pid: u32, path_cache: &mut PathCache) -> Vec<FdObservation> {
    let Some(process) = lifecycle_token(pid) else {
        return Vec::new();
    };
    let Ok(fds) = std::fs::read_dir(format!("/proc/{pid}/fd")) else {
        return Vec::new();
    };
    let comm = read_comm(pid);
    let mut observations = Vec::new();
    for entry in fds.flatten() {
        let name_os = entry.file_name();
        let Some(name) = name_os.to_str() else {
            continue;
        };
        let Ok(fd_num) = name.parse::<i32>() else {
            continue;
        };
        let token = InvalidationToken {
            process,
            fd: fd_num,
        };
        let Ok(target) = std::fs::read_link(entry.path()) else {
            path_cache.invalidate(token);
            continue;
        };
        let target_str = target.to_string_lossy().to_string();
        if !path_cache.observe(token, &target_str) {
            continue;
        }
        // Keep the feed useful by omitting kernel-internal descriptors.
        if target_str.starts_with("anon_inode:")
            || target_str.starts_with("pipe:[")
            || target_str == "/dev/null"
            || target_str.starts_with("/dev/pts/")
        {
            continue;
        }
        if observations.len() >= MAX_EVENTS_PER_TICK {
            break;
        }
        observations.push(FdObservation {
            pid,
            fd: fd_num,
            access: fd_access(pid, fd_num),
            path: target_str,
            comm: comm.clone(),
        });
    }
    observations
}

/// Explicit alias for callers that want the process/fd terminology in a
/// benchmark name without depending on the poller's event bus.
pub fn scan_process_fds(pid: u32, path_cache: &mut PathCache) -> Vec<FdObservation> {
    scan_fds(pid, path_cache)
}

/// Poll interval selected from the time since the last observed activity.
pub fn poll_interval_ms(idle_for: Duration) -> u64 {
    let idle_ms = idle_for.as_millis() as u64;
    if idle_ms >= LONG_IDLE_AFTER_MS {
        LONG_IDLE_POLL_INTERVAL_MS
    } else if idle_ms >= IDLE_AFTER_MS {
        IDLE_POLL_INTERVAL_MS
    } else {
        ACTIVE_POLL_INTERVAL_MS
    }
}

/// Capture a PID plus Linux's monotonic process start time. A start time is
/// the lifecycle invalidation token that prevents PID reuse confusion.
pub fn lifecycle_token(pid: u32) -> Option<LifecycleToken> {
    Some(LifecycleToken {
        pid,
        start_time: process_start_time(pid)?,
    })
}

/// Spawn the poller task. `roots` are outer-visible pids of the sandbox.
pub fn spawn_poller(bus: EventBus, roots: Vec<u32>) {
    let roots: Vec<LifecycleToken> = roots.into_iter().filter_map(lifecycle_token).collect();
    if roots.is_empty() {
        return;
    }
    spawn_poller_with_lifecycle(bus, roots);
}

pub fn spawn_poller_with_lifecycle(bus: EventBus, roots: Vec<LifecycleToken>) {
    if roots.is_empty() {
        return;
    }
    std::thread::Builder::new()
        .name("vetto-visibility".into())
        .spawn(move || poll_loop(bus, roots))
        .expect("spawn visibility thread");
}

fn poll_loop(bus: EventBus, roots: Vec<LifecycleToken>) {
    let root_pids: Vec<u32> = roots.iter().map(|root| root.pid).collect();
    let mut path_cache = PathCache::default();
    let mut seen_pids: HashSet<LifecycleToken> = HashSet::new();
    let mut known_processes: HashSet<LifecycleToken> = HashSet::new();
    let mut last_activity = Instant::now();

    loop {
        if !roots.iter().any(process_is_alive) {
            break;
        }
        let tree = collect_subtree(&root_pids);
        let mut active_processes = HashSet::new();
        let mut process_tokens = Vec::new();
        for pid in tree {
            let Some(token) = lifecycle_token(pid) else {
                continue;
            };
            active_processes.insert(token);
            process_tokens.push(token);
        }
        for old in known_processes.difference(&active_processes) {
            path_cache.invalidate_process(*old);
            seen_pids.remove(old);
        }
        known_processes = active_processes;
        let mut emitted = 0usize;
        let mut activity = false;

        for process in &process_tokens {
            let pid = process.pid;
            // New process appeared?
            if seen_pids.insert(*process) {
                let argv = read_cmdline(pid);
                bus.publish(Event::ExecObserved {
                    ts: crate::events::types::now(),
                    pid,
                    argv,
                });
                activity = true;
            }

            for observation in scan_fds(pid, &mut path_cache) {
                if emitted >= MAX_EVENTS_PER_TICK {
                    break; // drop silently; documented best-effort behavior
                }
                emitted += 1;
                bus.publish(Event::FileObserved {
                    ts: crate::events::types::now(),
                    pid: observation.pid,
                    comm: observation.comm,
                    path: observation.path,
                    access: observation.access,
                });
                activity = true;
            }
            if emitted >= MAX_EVENTS_PER_TICK {
                break;
            }
        }

        if activity {
            last_activity = Instant::now();
        }
        let interval = poll_interval_ms(last_activity.elapsed());
        std::thread::sleep(Duration::from_millis(interval));
        if path_cache.len() > MAX_PATH_CACHE_ENTRIES {
            path_cache.clear();
        }
    }
}

fn process_is_alive(token: &LifecycleToken) -> bool {
    let Some(current) = lifecycle_token(token.pid) else {
        return false;
    };
    (token.start_time != 0 && token.start_time == current.start_time)
        && !process_is_zombie(token.pid)
}

fn process_is_zombie(pid: u32) -> bool {
    let Ok(status) = std::fs::read_to_string(format!("/proc/{pid}/status")) else {
        return true;
    };
    status
        .lines()
        .find(|line| line.starts_with("State:"))
        .map(|line| line["State:".len()..].trim_start().starts_with('Z'))
        .unwrap_or(true)
}

/// Read the kernel-assigned process start time from `/proc/<pid>/stat`.
///
/// The second field (`comm`) is parenthesized and may itself contain spaces,
/// so split only after the final `) ` before indexing the remaining fields.
/// Field 22 is `starttime`; after field 2 has been removed it is index 19.
fn process_start_time(pid: u32) -> Option<u64> {
    let stat = std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
    let end_comm = stat.rfind(") ")?;
    stat[end_comm + 2..]
        .split_whitespace()
        .nth(19)?
        .parse()
        .ok()
}

/// BFS over PPid links from the sandbox roots.
pub fn collect_subtree(roots: &[u32]) -> Vec<u32> {
    let mut children: HashMap<u32, Vec<u32>> = HashMap::new();
    if let Ok(entries) = std::fs::read_dir("/proc") {
        for entry in entries.flatten() {
            let name = entry.file_name();
            let Some(pid) = name.to_str().and_then(|s| s.parse::<u32>().ok()) else {
                continue;
            };
            if let Some(ppid) = read_ppid(pid) {
                children.entry(ppid).or_default().push(pid);
            }
        }
    }
    let mut out = Vec::new();
    let mut queue: std::collections::VecDeque<u32> = roots.iter().copied().collect();
    let mut visited: HashSet<u32> = roots.iter().copied().collect();
    while let Some(pid) = queue.pop_front() {
        out.push(pid);
        if let Some(descendants) = children.get(&pid) {
            for &child in descendants {
                if !visited.insert(child) {
                    continue;
                }
                queue.push_back(child);
            }
        }
    }
    out
}

fn read_ppid(pid: u32) -> Option<u32> {
    let status = std::fs::read_to_string(format!("/proc/{pid}/status")).ok()?;
    let line = status.lines().find(|l| l.starts_with("PPid:"))?;
    line["PPid:".len()..].trim().parse().ok()
}

fn read_comm(pid: u32) -> String {
    std::fs::read_to_string(format!("/proc/{pid}/comm"))
        .map(|s| s.trim().to_string())
        .unwrap_or_else(|_| "?".into())
}

fn read_cmdline(pid: u32) -> Vec<String> {
    std::fs::read_to_string(format!("/proc/{pid}/cmdline"))
        .map(|s| {
            s.split('\0')
                .filter(|a| !a.is_empty())
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default()
}

/// Parse `flags:` from fdinfo to distinguish read vs write opens.
fn fd_access(pid: u32, fd: i32) -> FileAccess {
    let Ok(info) = std::fs::read_to_string(format!("/proc/{pid}/fdinfo/{fd}")) else {
        return FileAccess::Unknown;
    };
    let Some(line) = info.lines().find(|l| l.starts_with("flags:")) else {
        return FileAccess::Unknown;
    };
    let Ok(flags) = u32::from_str_radix(line["flags:".len()..].trim(), 8) else {
        return FileAccess::Unknown;
    };
    match flags & 0b11 {
        0 => FileAccess::Read,      // O_RDONLY
        1 | 2 => FileAccess::Write, // O_WRONLY | O_RDWR
        _ => FileAccess::Unknown,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn poller_adapts_after_idle_windows() {
        assert_eq!(
            poll_interval_ms(Duration::from_millis(0)),
            ACTIVE_POLL_INTERVAL_MS
        );
        assert_eq!(
            poll_interval_ms(Duration::from_millis(IDLE_AFTER_MS)),
            IDLE_POLL_INTERVAL_MS
        );
        assert_eq!(
            poll_interval_ms(Duration::from_millis(LONG_IDLE_AFTER_MS)),
            LONG_IDLE_POLL_INTERVAL_MS
        );
    }

    #[test]
    fn path_cache_is_bounded_lru_and_explicitly_invalidatable() {
        let process = LifecycleToken {
            pid: 7,
            start_time: 11,
        };
        let mut cache = PathCache::new(2);
        let first = InvalidationToken { process, fd: 1 };
        let second = InvalidationToken { process, fd: 2 };
        let third = InvalidationToken { process, fd: 3 };

        assert!(cache.observe(first, "/tmp/a"));
        assert!(!cache.observe(first, "/tmp/a"));
        assert!(cache.observe(first, "/tmp/b"));
        assert!(cache.observe(second, "/tmp/c"));
        assert!(cache.observe(third, "/tmp/d"));
        assert_eq!(cache.capacity(), 2);
        assert!(cache.len() <= cache.capacity());
        for _ in 0..128 {
            assert!(!cache.observe(third, "/tmp/d"));
        }
        assert!(cache.lru.len() <= cache.capacity().saturating_mul(2).max(64));

        cache.invalidate(third);
        assert!(!cache.entries.contains_key(&third));
        cache.invalidate_process(process);
        assert!(cache.is_empty());
    }

    #[test]
    fn lifecycle_token_is_stable_for_current_process() {
        let pid = std::process::id();
        let first = lifecycle_token(pid).expect("current process has a /proc stat");
        let second = lifecycle_token(pid).expect("current process has a /proc stat");
        assert_eq!(first, second);
        assert!(first.start_time > 0);
    }
}