sandbox-seccomp 0.2.1

Seccomp BPF filtering for sandbox-rs (no root required)
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
685
686
687
688
//! Seccomp filter building and management

use sandbox_core::{Result, SandboxError};
use std::collections::HashSet;

/// Seccomp filter profile.
///
/// Each profile includes all syscalls from profiles below it (cumulative):
/// `Essential < Minimal < IoHeavy < Compute < Network < Unrestricted`
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SeccompProfile {
    /// Essential — only the ~40 syscalls needed for process bootstrap (linker, glibc init, exit)
    Essential,
    /// Minimal — Essential + signals, pipes, timers, process control (~110 total)
    Minimal,
    /// IO-heavy — Minimal + file manipulation (mkdir, chmod, rename, fsync, …)
    IoHeavy,
    /// Compute — IoHeavy + advanced scheduling and NUMA (sched_setscheduler, mbind, …)
    Compute,
    /// Network — Compute + sockets (socket, bind, listen, connect, …)
    Network,
    /// Unrestricted — Network + privileged ops (ptrace, mount, bpf, setuid, …)
    Unrestricted,
}

impl SeccompProfile {
    /// Get all profiles
    pub fn all() -> Vec<Self> {
        vec![
            SeccompProfile::Essential,
            SeccompProfile::Minimal,
            SeccompProfile::IoHeavy,
            SeccompProfile::Compute,
            SeccompProfile::Network,
            SeccompProfile::Unrestricted,
        ]
    }

    /// Get description of profile
    pub fn description(&self) -> &'static str {
        match self {
            SeccompProfile::Essential => "Process bootstrap only (~40 syscalls)",
            SeccompProfile::Minimal => "Essential + signals, pipes, timers, process control",
            SeccompProfile::IoHeavy => "Minimal + file manipulation (mkdir, chmod, rename, …)",
            SeccompProfile::Compute => "IoHeavy + advanced scheduling/NUMA",
            SeccompProfile::Network => "Compute + socket operations",
            SeccompProfile::Unrestricted => "Network + privileged operations",
        }
    }
}

/// Seccomp filter builder
#[derive(Debug, Clone)]
pub struct SeccompFilter {
    allowed: HashSet<String>,
    blocked: HashSet<String>,
    kill_on_violation: bool,
    profile: SeccompProfile,
}

impl SeccompFilter {
    /// Create filter from profile
    pub fn from_profile(profile: SeccompProfile) -> Self {
        let allowed = Self::syscalls_for_profile(&profile);
        Self {
            allowed,
            blocked: HashSet::new(),
            kill_on_violation: true,
            profile,
        }
    }

    /// Create minimal filter
    pub fn minimal() -> Self {
        Self::from_profile(SeccompProfile::Minimal)
    }

    /// Syscalls needed for process bootstrap (linker, glibc init, exit).
    fn essential_syscalls() -> Vec<&'static str> {
        vec![
            // Lifecycle
            "exit",
            "exit_group",
            // Exec
            "execve",
            "execveat",
            // Memory (linker)
            "brk",
            "mmap",
            "munmap",
            "mprotect",
            "madvise",
            // File (linker)
            "openat",
            "open",
            "read",
            "write",
            "close",
            "close_range",
            // Stat
            "fstat",
            "stat",
            "lstat",
            "newfstatat",
            "statx",
            // Access
            "access",
            "faccessat",
            "faccessat2",
            // Seek
            "lseek",
            // Links
            "readlink",
            "readlinkat",
            // glibc init
            "arch_prctl",
            "set_tid_address",
            "set_robust_list",
            "futex",
            "getrandom",
            "rseq",
            "prlimit64",
            "prctl",
            // CWD
            "getcwd",
            // Identity
            "getpid",
            "gettid",
            "getuid",
            "geteuid",
            "getgid",
            "getegid",
            // FD
            "fcntl",
        ]
    }

    /// Extra syscalls for a typical program (signals, pipes, timers, etc).
    fn minimal_extras() -> Vec<&'static str> {
        vec![
            // Signals
            "rt_sigaction",
            "rt_sigprocmask",
            "rt_sigpending",
            "rt_sigtimedwait",
            "rt_sigqueueinfo",
            "rt_sigreturn",
            "sigaltstack",
            "kill",
            "tkill",
            "tgkill",
            // Processes
            "clone",
            "clone3",
            "fork",
            "vfork",
            "wait4",
            "waitpid",
            "waitid",
            // I/O avançado
            "readv",
            "writev",
            "pread64",
            "pwrite64",
            "ioctl",
            "flock",
            // FDs
            "dup",
            "dup2",
            "dup3",
            "pipe",
            "pipe2",
            "eventfd2",
            // Time
            "clock_gettime",
            "clock_getres",
            "gettimeofday",
            "time",
            "nanosleep",
            "clock_nanosleep",
            // Timers
            "timer_create",
            "timer_settime",
            "timer_gettime",
            "timer_getoverrun",
            "timer_delete",
            // Info
            "getppid",
            "getresuid",
            "getresgid",
            "uname",
            "umask",
            "sysinfo",
            "getpgrp",
            "getpgid",
            "setpgid",
            "getsid",
            "setsid",
            // Scheduling
            "sched_getaffinity",
            "sched_yield",
            // Limits
            "getrlimit",
            "setrlimit",
            "getrusage",
            // Polling
            "pselect6",
            "ppoll",
            "epoll_create1",
            "epoll_ctl",
            "epoll_wait",
            "poll",
            "select",
            // Dir
            "chdir",
            "fchdir",
            "getdents",
            "getdents64",
            // Memory
            "mremap",
            "mlock",
            "munlock",
            "mlockall",
            "munlockall",
            "memfd_create",
            // Misc
            "get_robust_list",
        ]
    }

    /// Extra syscalls for file manipulation.
    fn io_heavy_extras() -> Vec<&'static str> {
        vec![
            "mkdir",
            "mkdirat",
            "rmdir",
            "unlink",
            "unlinkat",
            "rename",
            "renameat",
            "link",
            "linkat",
            "symlink",
            "symlinkat",
            "chmod",
            "fchmod",
            "fchmodat",
            "chown",
            "fchown",
            "fchownat",
            "lchown",
            "utimes",
            "futimesat",
            "utime",
            "utimensat",
            "truncate",
            "ftruncate",
            "fallocate",
            "sendfile",
            "splice",
            "tee",
            "vmsplice",
            "statfs",
            "fstatfs",
            "fsync",
            "fdatasync",
        ]
    }

    /// Extra syscalls for compute-intensive workloads.
    fn compute_extras() -> Vec<&'static str> {
        vec![
            "sched_getscheduler",
            "sched_setscheduler",
            "sched_getparam",
            "sched_setparam",
            "sched_get_priority_max",
            "sched_get_priority_min",
            "sched_rr_get_interval",
            "sched_setaffinity",
            "mbind",
            "get_mempolicy",
            "set_mempolicy",
            "migrate_pages",
            "move_pages",
            "membarrier",
        ]
    }

    /// Extra syscalls for networking.
    fn network_extras() -> Vec<&'static str> {
        vec![
            "socket",
            "socketpair",
            "bind",
            "listen",
            "accept",
            "accept4",
            "connect",
            "shutdown",
            "sendto",
            "recvfrom",
            "sendmsg",
            "recvmsg",
            "sendmmsg",
            "recvmmsg",
            "setsockopt",
            "getsockopt",
            "getsockname",
            "getpeername",
        ]
    }

    /// Extra syscalls for unrestricted / privileged mode.
    fn unrestricted_extras() -> Vec<&'static str> {
        vec![
            "ptrace",
            "process_vm_readv",
            "process_vm_writev",
            "perf_event_open",
            "bpf",
            "seccomp",
            "mount",
            "umount2",
            "pivot_root",
            "capget",
            "capset",
            "setuid",
            "setgid",
            "setreuid",
            "setregid",
            "setresuid",
            "setresgid",
            "getgroups",
            "setgroups",
            "setfsgid",
            "setfsuid",
        ]
    }

    /// Get syscalls for a profile (cumulative).
    fn syscalls_for_profile(profile: &SeccompProfile) -> HashSet<String> {
        let mut syscalls = HashSet::new();

        let mut add = |list: Vec<&str>| {
            for s in list {
                syscalls.insert(s.to_string());
            }
        };

        // Cumulative: each level includes all levels below it
        add(Self::essential_syscalls());

        if matches!(
            profile,
            SeccompProfile::Minimal
                | SeccompProfile::IoHeavy
                | SeccompProfile::Compute
                | SeccompProfile::Network
                | SeccompProfile::Unrestricted
        ) {
            add(Self::minimal_extras());
        }

        if matches!(
            profile,
            SeccompProfile::IoHeavy
                | SeccompProfile::Compute
                | SeccompProfile::Network
                | SeccompProfile::Unrestricted
        ) {
            add(Self::io_heavy_extras());
        }

        if matches!(
            profile,
            SeccompProfile::Compute | SeccompProfile::Network | SeccompProfile::Unrestricted
        ) {
            add(Self::compute_extras());
        }

        if matches!(
            profile,
            SeccompProfile::Network | SeccompProfile::Unrestricted
        ) {
            add(Self::network_extras());
        }

        if matches!(profile, SeccompProfile::Unrestricted) {
            add(Self::unrestricted_extras());
        }

        syscalls
    }

    /// Add syscall to whitelist
    pub fn allow_syscall(&mut self, name: impl Into<String>) {
        self.allowed.insert(name.into());
    }

    /// Block a syscall (deny even if in whitelist)
    pub fn block_syscall(&mut self, name: impl Into<String>) {
        self.blocked.insert(name.into());
    }

    /// Check if syscall is allowed
    pub fn is_allowed(&self, name: &str) -> bool {
        if self.blocked.contains(name) {
            return false;
        }
        self.allowed.contains(name)
    }

    /// Get allowed syscalls
    pub fn allowed_syscalls(&self) -> &HashSet<String> {
        &self.allowed
    }

    /// Get blocked syscalls
    pub fn blocked_syscalls(&self) -> &HashSet<String> {
        &self.blocked
    }

    /// Count allowed syscalls
    pub fn allowed_count(&self) -> usize {
        self.allowed.len() - self.blocked.len()
    }

    /// Check if killing on violation
    pub fn is_kill_on_violation(&self) -> bool {
        self.kill_on_violation
    }

    /// Set kill on violation
    pub fn set_kill_on_violation(&mut self, kill: bool) {
        self.kill_on_violation = kill;
    }

    /// Get the profile used to create this filter
    pub fn profile(&self) -> SeccompProfile {
        self.profile.clone()
    }

    /// Validate that filter is correct
    pub fn validate(&self) -> Result<()> {
        if self.allowed.is_empty() && self.profile != SeccompProfile::Unrestricted {
            return Err(SandboxError::Seccomp(
                "Filter has no allowed syscalls".to_string(),
            ));
        }
        Ok(())
    }

    /// Export as BPF program (simplified - just returns syscall names)
    pub fn export(&self) -> Result<Vec<String>> {
        self.validate()?;
        let mut list: Vec<_> = self.allowed.iter().cloned().collect();
        list.sort();
        Ok(list)
    }
}

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

    #[test]
    fn test_seccomp_profile_all() {
        let profiles = SeccompProfile::all();
        assert_eq!(profiles.len(), 6);
    }

    #[test]
    fn test_seccomp_profile_description() {
        assert!(!SeccompProfile::Essential.description().is_empty());
        assert!(!SeccompProfile::Minimal.description().is_empty());
        assert_ne!(
            SeccompProfile::Essential.description(),
            SeccompProfile::Minimal.description()
        );
        assert_ne!(
            SeccompProfile::Minimal.description(),
            SeccompProfile::Network.description()
        );
    }

    #[test]
    fn test_seccomp_filter_essential() {
        let filter = SeccompFilter::from_profile(SeccompProfile::Essential);
        // Bootstrap syscalls
        assert!(filter.is_allowed("read"));
        assert!(filter.is_allowed("write"));
        assert!(filter.is_allowed("exit"));
        assert!(filter.is_allowed("execve"));
        assert!(filter.is_allowed("mmap"));
        assert!(filter.is_allowed("brk"));
        assert!(filter.is_allowed("openat"));
        assert!(filter.is_allowed("close"));
        assert!(filter.is_allowed("arch_prctl"));
        assert!(filter.is_allowed("futex"));
        assert!(filter.is_allowed("getpid"));
        assert!(filter.is_allowed("gettid"));
        assert!(filter.is_allowed("lseek"));
        assert!(filter.is_allowed("fcntl"));

        // NOT in Essential
        assert!(!filter.is_allowed("clone"));
        assert!(!filter.is_allowed("rt_sigaction"));
        assert!(!filter.is_allowed("nanosleep"));
        assert!(!filter.is_allowed("socket"));
        assert!(!filter.is_allowed("ptrace"));
        assert!(!filter.is_allowed("mkdir"));

        let count = filter.allowed_count();
        assert!(
            (35..=50).contains(&count),
            "Essential profile should have ~40 syscalls, got {}",
            count
        );
    }

    #[test]
    fn test_seccomp_filter_minimal() {
        let filter = SeccompFilter::minimal();
        assert!(filter.is_allowed("read"));
        assert!(filter.is_allowed("write"));
        assert!(filter.is_allowed("exit"));
        assert!(filter.is_allowed("clone3"));
        assert!(filter.is_allowed("lseek"));
        assert!(filter.is_allowed("sched_getaffinity"));
        assert!(filter.is_allowed("nanosleep"));
        assert!(filter.is_allowed("gettid"));
        assert!(filter.is_allowed("rt_sigaction"));
        assert!(!filter.is_allowed("ptrace"));
        assert!(!filter.is_allowed("mkdir"));
        assert!(!filter.is_allowed("socket"));
        assert!(
            filter.allowed_count() > 100,
            "Minimal profile should have > 100 syscalls for runtime compatibility, got {}",
            filter.allowed_count()
        );
    }

    #[test]
    fn test_seccomp_filter_io_heavy() {
        let filter = SeccompFilter::from_profile(SeccompProfile::IoHeavy);
        assert!(filter.is_allowed("read"));
        assert!(filter.is_allowed("mkdir"));
        assert!(filter.is_allowed("unlink"));
        // Also has Minimal extras (cumulative)
        assert!(filter.is_allowed("clone"));
        assert!(filter.is_allowed("rt_sigaction"));
        let io_count = filter.allowed_count();

        let minimal = SeccompFilter::minimal();
        assert!(io_count > minimal.allowed_count());
    }

    #[test]
    fn test_seccomp_filter_network() {
        let filter = SeccompFilter::from_profile(SeccompProfile::Network);
        assert!(filter.is_allowed("socket"));
        assert!(filter.is_allowed("connect"));
        assert!(filter.is_allowed("bind"));
        // Cumulative: also has IoHeavy extras
        assert!(filter.is_allowed("mkdir"));
        // Cumulative: also has Compute extras
        assert!(filter.is_allowed("sched_setscheduler"));
    }

    #[test]
    fn test_seccomp_filter_allow_syscall() {
        let mut filter = SeccompFilter::minimal();
        filter.allow_syscall("custom_syscall");
        assert!(filter.is_allowed("custom_syscall"));
    }

    #[test]
    fn test_seccomp_filter_block_syscall() {
        let mut filter = SeccompFilter::minimal();
        filter.block_syscall("read");
        assert!(!filter.is_allowed("read"));
    }

    #[test]
    fn test_seccomp_filter_block_overrides_allow() {
        let mut filter = SeccompFilter::minimal();
        assert!(filter.is_allowed("write"));
        filter.block_syscall("write");
        assert!(!filter.is_allowed("write"));
    }

    #[test]
    fn test_seccomp_filter_validate() {
        let filter = SeccompFilter::minimal();
        assert!(filter.validate().is_ok());

        let empty_filter = SeccompFilter {
            allowed: HashSet::new(),
            blocked: HashSet::new(),
            kill_on_violation: true,
            profile: SeccompProfile::Minimal,
        };
        assert!(empty_filter.validate().is_err());
    }

    #[test]
    fn test_seccomp_filter_export() {
        let filter = SeccompFilter::minimal();
        let syscalls = filter.export().unwrap();
        assert!(!syscalls.is_empty());
        assert!(syscalls.contains(&"read".to_string()));

        // Should be sorted
        let mut sorted = syscalls.clone();
        sorted.sort();
        assert_eq!(syscalls, sorted);
    }

    #[test]
    fn test_seccomp_kill_on_violation() {
        let mut filter = SeccompFilter::minimal();
        assert!(filter.is_kill_on_violation());

        filter.set_kill_on_violation(false);
        assert!(!filter.is_kill_on_violation());
    }

    #[test]
    fn test_validate_unrestricted_with_no_allowed() {
        let filter = SeccompFilter {
            allowed: HashSet::new(),
            blocked: HashSet::new(),
            kill_on_violation: true,
            profile: SeccompProfile::Unrestricted,
        };
        assert!(filter.validate().is_ok());
    }

    #[test]
    fn test_profiles_are_cumulative() {
        let essential = SeccompFilter::from_profile(SeccompProfile::Essential);
        let minimal = SeccompFilter::from_profile(SeccompProfile::Minimal);
        let io_heavy = SeccompFilter::from_profile(SeccompProfile::IoHeavy);
        let compute = SeccompFilter::from_profile(SeccompProfile::Compute);
        let network = SeccompFilter::from_profile(SeccompProfile::Network);
        let unrestricted = SeccompFilter::from_profile(SeccompProfile::Unrestricted);

        // Each profile must be a strict superset of the one below
        assert!(
            essential
                .allowed_syscalls()
                .is_subset(minimal.allowed_syscalls()),
            "Essential should be a subset of Minimal"
        );
        assert!(
            minimal
                .allowed_syscalls()
                .is_subset(io_heavy.allowed_syscalls()),
            "Minimal should be a subset of IoHeavy"
        );
        assert!(
            io_heavy
                .allowed_syscalls()
                .is_subset(compute.allowed_syscalls()),
            "IoHeavy should be a subset of Compute"
        );
        assert!(
            compute
                .allowed_syscalls()
                .is_subset(network.allowed_syscalls()),
            "Compute should be a subset of Network"
        );
        assert!(
            network
                .allowed_syscalls()
                .is_subset(unrestricted.allowed_syscalls()),
            "Network should be a subset of Unrestricted"
        );

        // And strictly more syscalls at each level
        assert!(minimal.allowed_count() > essential.allowed_count());
        assert!(io_heavy.allowed_count() > minimal.allowed_count());
        assert!(compute.allowed_count() > io_heavy.allowed_count());
        assert!(network.allowed_count() > compute.allowed_count());
        assert!(unrestricted.allowed_count() > network.allowed_count());
    }
}