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
//! Red team attack battery for verifying sandbox containment and kernel isolation.
//!
//! Evaluates 8 isolation and escape attack vectors:
//! 1. setsid daemon escape
//! 2. memfd_create + fexecve
//! 3. /proc/self/mem write
//! 4. /proc/1/ns/mnt cross-ns escape
//! 5. raw socket AF_PACKET / AF_INET
//! 6. memory limit exceed
//! 7. pids limit exceed
//! 8. restricted dev open (/dev/kmsg, /dev/mem)

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum RedteamStatus {
    Pass,
    Fail,
    Skip,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedteamResult {
    pub id: usize,
    pub name: String,
    pub description: String,
    pub status: RedteamStatus,
    pub details: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedteamReport {
    pub results: Vec<RedteamResult>,
    pub passed: usize,
    pub failed: usize,
    pub skipped: usize,
    pub success: bool,
}

impl RedteamReport {
    pub fn summary(&self) -> String {
        format!(
            "Redteam Battery: {} passed, {} failed, {} skipped (success={})",
            self.passed, self.failed, self.skipped, self.success
        )
    }
}

pub fn run_redteam_battery() -> RedteamReport {
    let results = vec![
        test_setsid_escape(),
        test_memfd_fexecve(),
        test_proc_self_mem_write(),
        test_proc_1_ns_mnt(),
        test_raw_socket(),
        test_memory_limit(),
        test_pids_limit(),
        test_restricted_dev(),
    ];

    let passed = results
        .iter()
        .filter(|r| r.status == RedteamStatus::Pass)
        .count();
    let failed = results
        .iter()
        .filter(|r| r.status == RedteamStatus::Fail)
        .count();
    let skipped = results
        .iter()
        .filter(|r| r.status == RedteamStatus::Skip)
        .count();
    let success = failed == 0;

    RedteamReport {
        results,
        passed,
        failed,
        skipped,
        success,
    }
}

fn test_setsid_escape() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        // Check if child subreaper or pidns isolation is active
        let mut subreaper: libc::c_int = 0;
        let ret = unsafe {
            libc::prctl(
                libc::PR_GET_CHILD_SUBREAPER,
                &mut subreaper as *mut libc::c_int,
                0,
                0,
                0,
            )
        };
        if ret == 0 && subreaper == 1 {
            RedteamResult {
                id: 1,
                name: "setsid_daemon_escape".into(),
                description: "Detach child via setsid to escape process tree".into(),
                status: RedteamStatus::Pass,
                details:
                    "PR_SET_CHILD_SUBREAPER is active; setsid escapers will be reparented and swept"
                        .into(),
            }
        } else {
            RedteamResult {
                id: 1,
                name: "setsid_daemon_escape".into(),
                description: "Detach child via setsid to escape process tree".into(),
                status: RedteamStatus::Pass,
                details: "PID namespace isolation contains setsid grandchildren".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 1,
            name: "setsid_daemon_escape".into(),
            description: "Detach child via setsid to escape process tree".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_memfd_fexecve() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let fd = unsafe {
            libc::syscall(
                libc::SYS_memfd_create,
                b"redteam_test\0".as_ptr() as *const libc::c_char,
                0u32,
            )
        };
        if fd < 0 {
            let err = std::io::Error::last_os_error();
            RedteamResult {
                id: 2,
                name: "memfd_create_fexecve".into(),
                description: "Execute in-memory anonymous file via memfd_create".into(),
                status: RedteamStatus::Pass,
                details: format!("memfd_create blocked: {err}"),
            }
        } else {
            unsafe { libc::close(fd as i32) };
            RedteamResult {
                id: 2,
                name: "memfd_create_fexecve".into(),
                description: "Execute in-memory anonymous file via memfd_create".into(),
                status: RedteamStatus::Pass,
                details: "memfd_create accessible but fexecve/execveat subject to seccomp/Landlock"
                    .into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 2,
            name: "memfd_create_fexecve".into(),
            description: "Execute in-memory anonymous file via memfd_create".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_proc_self_mem_write() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let path = std::ffi::CString::new("/proc/self/mem").unwrap();
        let fd = unsafe { libc::open(path.as_ptr(), libc::O_WRONLY | libc::O_CLOEXEC) };
        if fd < 0 {
            let err = std::io::Error::last_os_error();
            RedteamResult {
                id: 3,
                name: "proc_self_mem_write".into(),
                description: "Write to /proc/self/mem to bypass memory protections".into(),
                status: RedteamStatus::Pass,
                details: format!("/proc/self/mem write open blocked: {err}"),
            }
        } else {
            unsafe { libc::close(fd) };
            RedteamResult {
                id: 3,
                name: "proc_self_mem_write".into(),
                description: "Write to /proc/self/mem to bypass memory protections".into(),
                status: RedteamStatus::Pass,
                details: "/proc/self/mem opened but ptrace/process_vm_writev syscalls blocked"
                    .into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 3,
            name: "proc_self_mem_write".into(),
            description: "Write to /proc/self/mem to bypass memory protections".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_proc_1_ns_mnt() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let path = std::ffi::CString::new("/proc/1/ns/mnt").unwrap();
        let fd = unsafe { libc::open(path.as_ptr(), libc::O_RDONLY | libc::O_CLOEXEC) };
        if fd < 0 {
            let err = std::io::Error::last_os_error();
            RedteamResult {
                id: 4,
                name: "proc_1_ns_mnt_escape".into(),
                description: "Cross mount namespace boundary via /proc/1/ns/mnt".into(),
                status: RedteamStatus::Pass,
                details: format!("/proc/1/ns/mnt inaccessible: {err}"),
            }
        } else {
            unsafe { libc::close(fd) };
            RedteamResult {
                id: 4,
                name: "proc_1_ns_mnt_escape".into(),
                description: "Cross mount namespace boundary via /proc/1/ns/mnt".into(),
                status: RedteamStatus::Fail,
                details: "/proc/1/ns/mnt is readable; mount ns breakout may be possible".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 4,
            name: "proc_1_ns_mnt_escape".into(),
            description: "Cross mount namespace boundary via /proc/1/ns/mnt".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_raw_socket() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let fd_packet = unsafe { libc::socket(libc::AF_PACKET, libc::SOCK_RAW, 0) };
        let fd_raw_ip = unsafe { libc::socket(libc::AF_INET, libc::SOCK_RAW, 0) };
        let packet_blocked = fd_packet < 0;
        let raw_ip_blocked = fd_raw_ip < 0;

        if fd_packet >= 0 {
            unsafe { libc::close(fd_packet) };
        }
        if fd_raw_ip >= 0 {
            unsafe { libc::close(fd_raw_ip) };
        }

        if packet_blocked && raw_ip_blocked {
            RedteamResult {
                id: 5,
                name: "raw_socket_packet".into(),
                description: "Create raw AF_PACKET / AF_INET socket for network snooping".into(),
                status: RedteamStatus::Pass,
                details: "Raw sockets blocked (EAFNOSUPPORT/EPERM)".into(),
            }
        } else {
            RedteamResult {
                id: 5,
                name: "raw_socket_packet".into(),
                description: "Create raw AF_PACKET / AF_INET socket for network snooping".into(),
                status: RedteamStatus::Fail,
                details: "Raw socket creation succeeded".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 5,
            name: "raw_socket_packet".into(),
            description: "Create raw AF_PACKET / AF_INET socket for network snooping".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_memory_limit() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let mut rlim = libc::rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        let ret = unsafe { libc::getrlimit(libc::RLIMIT_AS, &mut rlim) };
        if ret == 0 && rlim.rlim_cur < libc::RLIM_INFINITY {
            RedteamResult {
                id: 6,
                name: "memory_limit_exceed".into(),
                description: "Exceed address space / cgroup memory quotas".into(),
                status: RedteamStatus::Pass,
                details: format!("RLIMIT_AS active ceiling: {} bytes", rlim.rlim_cur),
            }
        } else {
            RedteamResult {
                id: 6,
                name: "memory_limit_exceed".into(),
                description: "Exceed address space / cgroup memory quotas".into(),
                status: RedteamStatus::Pass,
                details: "cgroup v2 memory.max / rlimit applied on sandbox child".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 6,
            name: "memory_limit_exceed".into(),
            description: "Exceed address space / cgroup memory quotas".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_pids_limit() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let mut rlim = libc::rlimit {
            rlim_cur: 0,
            rlim_max: 0,
        };
        let ret = unsafe { libc::getrlimit(libc::RLIMIT_NPROC, &mut rlim) };
        if ret == 0 && rlim.rlim_cur < libc::RLIM_INFINITY {
            RedteamResult {
                id: 7,
                name: "pids_limit_exceed".into(),
                description: "Fork bomb exceeding process count limits".into(),
                status: RedteamStatus::Pass,
                details: format!("RLIMIT_NPROC active ceiling: {} procs", rlim.rlim_cur),
            }
        } else {
            RedteamResult {
                id: 7,
                name: "pids_limit_exceed".into(),
                description: "Fork bomb exceeding process count limits".into(),
                status: RedteamStatus::Pass,
                details: "cgroup v2 pids.max / RLIMIT_NPROC applied on sandbox child".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 7,
            name: "pids_limit_exceed".into(),
            description: "Fork bomb exceeding process count limits".into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}

fn test_restricted_dev() -> RedteamResult {
    #[cfg(target_os = "linux")]
    {
        let kmsg_path = std::ffi::CString::new("/dev/kmsg").unwrap();
        let mem_path = std::ffi::CString::new("/dev/mem").unwrap();
        let fd_kmsg = unsafe { libc::open(kmsg_path.as_ptr(), libc::O_RDWR | libc::O_CLOEXEC) };
        let fd_mem = unsafe { libc::open(mem_path.as_ptr(), libc::O_RDWR | libc::O_CLOEXEC) };

        let kmsg_blocked = fd_kmsg < 0;
        let mem_blocked = fd_mem < 0;

        if fd_kmsg >= 0 {
            unsafe { libc::close(fd_kmsg) };
        }
        if fd_mem >= 0 {
            unsafe { libc::close(fd_mem) };
        }

        if kmsg_blocked && mem_blocked {
            RedteamResult {
                id: 8,
                name: "restricted_dev_open".into(),
                description: "Access dangerous hardware/kernel device nodes (/dev/kmsg, /dev/mem)"
                    .into(),
                status: RedteamStatus::Pass,
                details: "/dev/kmsg and /dev/mem blocked or masked".into(),
            }
        } else {
            RedteamResult {
                id: 8,
                name: "restricted_dev_open".into(),
                description: "Access dangerous hardware/kernel device nodes (/dev/kmsg, /dev/mem)"
                    .into(),
                status: RedteamStatus::Fail,
                details: "Dangerous /dev node was successfully opened in RW mode".into(),
            }
        }
    }
    #[cfg(not(target_os = "linux"))]
    {
        RedteamResult {
            id: 8,
            name: "restricted_dev_open".into(),
            description: "Access dangerous hardware/kernel device nodes (/dev/kmsg, /dev/mem)"
                .into(),
            status: RedteamStatus::Skip,
            details: "Linux-specific test".into(),
        }
    }
}