#[cfg(feature = "seccomp")]
pub(super) extern "C" fn handle_sigsys(
signal: i32,
info: *mut libc::siginfo_t,
context: *mut libc::c_void,
) {
#[cfg(target_arch = "x86_64")]
{
unsafe {
const SYS_SECCOMP: libc::c_int = 1;
if signal != libc::SIGSYS
|| (*info).si_code != SYS_SECCOMP
|| context.is_null()
|| (*info).si_errno < 0
{
let err_msg =
b"[ERROR][HYPERLIGHT] SIGSYS triggered by something other than a BPF filter\n";
libc::write(
libc::STDERR_FILENO,
err_msg.as_ptr() as *const _,
err_msg.len(),
);
return;
}
let err_msg = b"[ERROR][HYPERLIGHT] Handling disallowed syscall\n";
libc::write(
libc::STDERR_FILENO,
err_msg.as_ptr() as *const _,
err_msg.len(),
);
const SI_OFF_SYSCALL: isize = 6;
let syscall = *(info as *const i32).offset(SI_OFF_SYSCALL) as usize;
let syscall_bytes = raw_format(b"[ERROR][HYPERLIGHT] Disallowed Syscall: ", syscall);
libc::write(
libc::STDERR_FILENO,
syscall_bytes.as_ptr() as *const _,
syscall_bytes.len(),
);
let ucontext = context as *mut libc::ucontext_t;
let mcontext = &mut (*ucontext).uc_mcontext;
if syscall == libc::SYS_ioctl as usize {
let ioctl_param = mcontext.gregs[libc::REG_EBRACE as usize] as usize;
let ioctl_param_bytes =
raw_format(b"[ERROR][HYPERLIGHT] IOCTL Param: ", ioctl_param);
libc::write(
libc::STDERR_FILENO,
ioctl_param_bytes.as_ptr() as *const _,
ioctl_param_bytes.len(),
);
}
mcontext.gregs[libc::REG_RIP as usize] =
after_syscall_violation as usize as libc::greg_t;
}
}
#[cfg(not(target_arch = "x86_64"))]
{
compile_error!("Unsupported architecture for seccomp feature");
}
}
extern "C-unwind" fn after_syscall_violation() {
std::panic::panic_any(crate::HyperlightError::DisallowedSyscall);
}
fn raw_format(prefix: &[u8], raw: usize) -> [u8; 64] {
const PREFIX_BUF_LEN: usize = 64;
const DIGITS_BUF_LEN: usize = 20;
let mut buffer = [0u8; PREFIX_BUF_LEN];
let mut i = prefix.len();
buffer[..i].copy_from_slice(prefix);
let mut num = raw;
let mut digits = [0u8; DIGITS_BUF_LEN];
let mut j = 19;
if num == 0 {
digits[j] = b'0';
j -= 1;
} else {
while num > 0 {
digits[j] = b'0' + (num % 10) as u8;
num /= 10;
j -= 1;
}
}
let num_len = 19 - j;
buffer[i..i + num_len].copy_from_slice(&digits[j + 1..20]);
i += num_len;
buffer[i] = b'\n';
buffer
}