use std::collections::BTreeMap;
use cant_policy::Policy;
use seccompiler::{
BpfProgram, SeccompAction, SeccompCmpArgLen as Len, SeccompCmpOp as Op, SeccompCondition,
SeccompFilter, SeccompRule, TargetArch, apply_filter,
};
use crate::LaunchError;
const SYS_FCHMODAT2: i64 = 452;
const SYS_SETXATTRAT: i64 = 463;
const SYS_REMOVEXATTRAT: i64 = 466;
#[cfg(target_arch = "x86_64")]
const SYS_KEXEC_FILE_LOAD: i64 = 320;
#[cfg(not(target_arch = "x86_64"))]
const SYS_KEXEC_FILE_LOAD: i64 = 294;
const CHMOD_FORBIDDEN_BITS: [u64; 4] = [0o4000, 0o2000, 0o1000, 0o002];
fn err(e: impl std::fmt::Display) -> LaunchError {
LaunchError::Seccomp(e.to_string())
}
fn target_arch() -> Result<TargetArch, LaunchError> {
if cfg!(target_arch = "x86_64") {
Ok(TargetArch::x86_64)
} else if cfg!(target_arch = "aarch64") {
Ok(TargetArch::aarch64)
} else if cfg!(target_arch = "riscv64") {
Ok(TargetArch::riscv64)
} else {
Err(LaunchError::UnsupportedArch(std::env::consts::ARCH))
}
}
fn denied_syscalls() -> Vec<i64> {
#[allow(unused_mut)]
let mut v = vec![
libc::SYS_ptrace,
libc::SYS_process_vm_readv,
libc::SYS_process_vm_writev,
libc::SYS_kexec_load,
SYS_KEXEC_FILE_LOAD,
libc::SYS_init_module,
libc::SYS_finit_module,
libc::SYS_delete_module,
libc::SYS_reboot,
libc::SYS_swapon,
libc::SYS_swapoff,
libc::SYS_acct,
libc::SYS_quotactl,
libc::SYS_settimeofday,
libc::SYS_clock_settime,
libc::SYS_clock_adjtime,
libc::SYS_adjtimex,
libc::SYS_syslog,
libc::SYS_vhangup,
libc::SYS_bpf,
libc::SYS_perf_event_open,
libc::SYS_userfaultfd,
libc::SYS_keyctl,
libc::SYS_add_key,
libc::SYS_request_key,
libc::SYS_io_uring_setup,
libc::SYS_io_uring_enter,
libc::SYS_io_uring_register,
libc::SYS_unshare,
libc::SYS_setns,
libc::SYS_mount,
libc::SYS_umount2,
libc::SYS_pivot_root,
libc::SYS_chroot,
libc::SYS_fsopen,
libc::SYS_fsconfig,
libc::SYS_fsmount,
libc::SYS_fspick,
libc::SYS_move_mount,
libc::SYS_open_tree,
libc::SYS_mount_setattr,
libc::SYS_open_by_handle_at,
libc::SYS_name_to_handle_at,
libc::SYS_fchownat,
libc::SYS_setxattr,
libc::SYS_lsetxattr,
libc::SYS_removexattr,
libc::SYS_lremovexattr,
SYS_SETXATTRAT,
SYS_REMOVEXATTRAT,
];
#[cfg(target_arch = "x86_64")]
v.extend([
libc::SYS_chown,
libc::SYS_lchown,
libc::SYS_utime,
libc::SYS_utimes,
libc::SYS_futimesat,
libc::SYS_iopl,
libc::SYS_ioperm,
libc::SYS_uselib,
]);
v
}
fn cond(arg: u8, len: Len, op: Op, value: u64) -> Result<SeccompCondition, LaunchError> {
SeccompCondition::new(arg, len, op, value).map_err(err)
}
fn rule(conditions: Vec<SeccompCondition>) -> Result<SeccompRule, LaunchError> {
SeccompRule::new(conditions).map_err(err)
}
fn eperm_filter(policy: &Policy, arch: TargetArch) -> Result<BpfProgram, LaunchError> {
let mut rules: BTreeMap<i64, Vec<SeccompRule>> = BTreeMap::new();
for nr in denied_syscalls() {
rules.insert(nr, Vec::new()); }
let mut clone_rules = Vec::new();
for flag in [
libc::CLONE_NEWNS,
libc::CLONE_NEWUTS,
libc::CLONE_NEWIPC,
libc::CLONE_NEWUSER,
libc::CLONE_NEWPID,
libc::CLONE_NEWNET,
libc::CLONE_NEWCGROUP,
] {
let flag = flag as u64;
clone_rules.push(rule(vec![cond(0, Len::Qword, Op::MaskedEq(flag), flag)?])?);
}
rules.insert(libc::SYS_clone, clone_rules);
#[allow(clippy::unnecessary_cast)]
let (tiocsti, tioclinux) = (libc::TIOCSTI as u64, libc::TIOCLINUX as u64);
rules.insert(
libc::SYS_ioctl,
vec![
rule(vec![cond(1, Len::Dword, Op::Eq, tiocsti)?])?,
rule(vec![cond(1, Len::Dword, Op::Eq, tioclinux)?])?,
],
);
let chmods: &[(i64, u8)] = &[
(libc::SYS_fchmodat, 2),
(SYS_FCHMODAT2, 2),
#[cfg(target_arch = "x86_64")]
(libc::SYS_chmod, 1),
];
for &(nr, mode_arg) in chmods {
let deny = if policy.chmod_by_path {
let mut r = Vec::new();
for bit in CHMOD_FORBIDDEN_BITS {
r.push(rule(vec![cond(
mode_arg,
Len::Dword,
Op::MaskedEq(bit),
bit,
)?])?);
}
r
} else {
Vec::new() };
rules.insert(nr, deny);
}
rules.insert(
libc::SYS_utimensat,
vec![rule(vec![cond(1, Len::Qword, Op::Ne, 0)?])?],
);
let af_unix = libc::AF_UNIX as u64;
match (policy.network, policy.unix_sockets) {
(false, false) => {
rules.insert(libc::SYS_socket, Vec::new());
}
(false, true) => {
rules.insert(
libc::SYS_socket,
vec![rule(vec![cond(0, Len::Dword, Op::Ne, af_unix)?])?],
);
}
(true, false) => {
rules.insert(
libc::SYS_socket,
vec![rule(vec![cond(0, Len::Dword, Op::Eq, af_unix)?])?],
);
}
(true, true) => {}
}
let filter = SeccompFilter::new(
rules,
SeccompAction::Allow,
SeccompAction::Errno(libc::EPERM as u32),
arch,
)
.map_err(err)?;
BpfProgram::try_from(filter).map_err(err)
}
fn clone3_filter(arch: TargetArch) -> Result<BpfProgram, LaunchError> {
let mut rules: BTreeMap<i64, Vec<SeccompRule>> = BTreeMap::new();
rules.insert(libc::SYS_clone3, Vec::new());
let filter = SeccompFilter::new(
rules,
SeccompAction::Allow,
SeccompAction::Errno(libc::ENOSYS as u32),
arch,
)
.map_err(err)?;
BpfProgram::try_from(filter).map_err(err)
}
#[cfg(target_arch = "x86_64")]
fn x32_guard() -> BpfProgram {
const LD_W_ABS: u16 = 0x20; const JEQ_K: u16 = 0x15; const JGE_K: u16 = 0x35; const RET_K: u16 = 0x06; const AUDIT_ARCH_X86_64: u32 = 0xC000_003E;
const X32_SYSCALL_BIT: u32 = 0x4000_0000;
const RET_ERRNO: u32 = 0x0005_0000;
const RET_ALLOW: u32 = 0x7fff_0000;
let ins = |code, jt, jf, k| seccompiler::sock_filter { code, jt, jf, k };
vec![
ins(LD_W_ABS, 0, 0, 4),
ins(JEQ_K, 0, 3, AUDIT_ARCH_X86_64),
ins(LD_W_ABS, 0, 0, 0),
ins(JGE_K, 0, 1, X32_SYSCALL_BIT),
ins(RET_K, 0, 0, RET_ERRNO | libc::EPERM as u32),
ins(RET_K, 0, 0, RET_ALLOW),
]
}
pub fn install(policy: &Policy) -> Result<(), LaunchError> {
let arch = target_arch()?;
#[cfg(target_arch = "x86_64")]
apply_filter(&x32_guard()).map_err(err)?;
apply_filter(&clone3_filter(arch)?).map_err(err)?;
apply_filter(&eperm_filter(policy, arch)?).map_err(err)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn filters_compile_for_every_policy_shape() {
let arch = target_arch().expect("supported arch");
for (network, unix_sockets) in [(false, false), (false, true), (true, false), (true, true)]
{
for chmod_by_path in [false, true] {
let p = Policy {
network,
unix_sockets,
chmod_by_path,
..Policy::default()
};
let prog = eperm_filter(&p, arch).expect("compiles");
assert!(!prog.is_empty());
assert!(prog.len() < 4096, "BPF_MAXINSNS");
}
}
assert!(!clone3_filter(arch).expect("compiles").is_empty());
}
#[test]
fn no_duplicate_denied_syscalls() {
let mut v = denied_syscalls();
let n = v.len();
v.sort_unstable();
v.dedup();
assert_eq!(v.len(), n);
}
}
#[cfg(test)]
mod syscall_number_tests {
#[test]
#[cfg(all(target_arch = "x86_64", target_env = "gnu"))]
fn kexec_file_load_matches_libc() {
assert_eq!(super::SYS_KEXEC_FILE_LOAD, libc::SYS_kexec_file_load);
}
}