use super::common::SandboxError;
pub fn enable() -> Result<bool, SandboxError> {
Ok(enable_platform_sandbox()?)
}
#[cfg(all(
target_os = "android",
target_endian = "little",
any(target_arch = "aarch64", target_arch = "x86_64")
))]
fn enable_platform_sandbox() -> seccompiler::Result<bool> {
use seccompiler::{
BpfProgram, SeccompAction, SeccompCmpArgLen, SeccompCmpOp, SeccompCondition, SeccompFilter,
SeccompRule,
};
use std::env::consts::ARCH;
const BINDER_WRITE_READ: u64 = 3224396289;
let filter: BpfProgram = SeccompFilter::new(
vec![
(libc::SYS_close, vec![]),
(libc::SYS_epoll_pwait, vec![]),
(
libc::SYS_fcntl,
vec![SeccompRule::new(vec![SeccompCondition::new(
1,
SeccompCmpArgLen::Dword,
SeccompCmpOp::Eq,
libc::F_DUPFD_CLOEXEC as u64,
)?])?],
),
(libc::SYS_getuid, vec![]),
(
libc::SYS_ioctl,
vec![SeccompRule::new(vec![SeccompCondition::new(
1,
SeccompCmpArgLen::Dword,
SeccompCmpOp::Eq,
BINDER_WRITE_READ,
)?])?],
),
(libc::SYS_lseek, vec![]),
(
libc::SYS_mmap,
vec![
SeccompRule::new(vec![SeccompCondition::new(
2,
SeccompCmpArgLen::Dword,
SeccompCmpOp::Eq,
(libc::PROT_READ | libc::PROT_WRITE) as u64,
)?])?,
SeccompRule::new(vec![SeccompCondition::new(
2,
SeccompCmpArgLen::Dword,
SeccompCmpOp::Eq,
libc::PROT_NONE as u64,
)?])?,
#[cfg(target_arch = "aarch64")]
SeccompRule::new(vec![SeccompCondition::new(
2,
SeccompCmpArgLen::Dword,
SeccompCmpOp::Eq,
libc::PROT_MTE as u64,
)?])?,
],
),
(libc::SYS_munmap, vec![]),
(libc::SYS_prctl, vec![]),
(libc::SYS_read, vec![]),
(libc::SYS_write, vec![]),
(libc::SYS_writev, vec![]),
]
.into_iter()
.collect(),
SeccompAction::KillProcess,
SeccompAction::Allow,
ARCH.try_into().unwrap(),
)?
.try_into()?;
seccompiler::apply_filter_all_threads(&filter)?;
Ok(true)
}
#[cfg(not(all(
target_os = "android",
target_endian = "little",
any(target_arch = "aarch64", target_arch = "x86_64")
)))]
fn enable_platform_sandbox() -> seccompiler::Result<bool> {
Ok(false)
}