#![forbid(unsafe_code)]
use libc::{c_int, c_uint};
use libseccomp::ScmpNotifResp;
use nix::{errno::Errno, sys::eventfd::EfdFlags};
use crate::{
cookie::{safe_eventfd, safe_exit_group},
log_enabled,
path::XPath,
req::UNotifyEventRequest,
sandbox::{Action, Capability, SandboxGuard},
syslog::LogLevel,
warn, xfmt,
};
pub(crate) fn sys_eventfd(request: UNotifyEventRequest) -> ScmpNotifResp {
syscall_handler!(request, |request: UNotifyEventRequest| {
#[expect(clippy::cast_possible_truncation)]
let count = request.scmpreq.data.args[0] as c_uint;
handle_eventfd(&request, count, EfdFlags::empty())
})
}
pub(crate) fn sys_eventfd2(request: UNotifyEventRequest) -> ScmpNotifResp {
syscall_handler!(request, |request: UNotifyEventRequest| {
let req = request.scmpreq;
let flags = to_efd_flags(req.data.args[1])?;
#[expect(clippy::cast_possible_truncation)]
let count = req.data.args[0] as c_uint;
handle_eventfd(&request, count, flags)
})
}
fn handle_eventfd(
request: &UNotifyEventRequest,
count: c_uint,
mut flags: EfdFlags,
) -> Result<ScmpNotifResp, Errno> {
let sandbox = request.get_sandbox();
let force_cloexec = sandbox.flags.force_cloexec();
let force_rand_fd = sandbox.flags.force_rand_fd();
sandbox_eventfd(request, &sandbox)?;
drop(sandbox);
let cloexec = force_cloexec || flags.contains(EfdFlags::EFD_CLOEXEC);
flags.insert(EfdFlags::EFD_CLOEXEC);
let fd = safe_eventfd(count, flags)?;
request.send_fd(fd, cloexec, force_rand_fd)
}
#[expect(clippy::cognitive_complexity)]
fn sandbox_eventfd(request: &UNotifyEventRequest, sandbox: &SandboxGuard<'_>) -> Result<(), Errno> {
let caps = Capability::CAP_CREATE;
let name = XPath::from_bytes(b"!eventfd");
if sandbox.getcaps(caps).is_empty() {
return Ok(());
}
let action = sandbox.check_name(caps, name);
if action.is_logging() && log_enabled!(LogLevel::Warn) {
if sandbox.log_scmp() {
warn!("ctx": "access", "cap": caps, "act": action,
"sys": request.syscall, "path": &name,
"tip": xfmt!("configure `allow/{caps}+{name}'"),
"req": request);
} else {
warn!("ctx": "access", "cap": caps, "act": action,
"sys": request.syscall, "path": &name,
"tip": xfmt!("configure `allow/{caps}+{name}'"),
"pid": request.scmpreq.pid);
}
}
match action {
Action::Allow | Action::Warn => Ok(()),
Action::Deny | Action::Filter => Err(Errno::ENODEV),
Action::Panic => panic!(),
Action::Exit => safe_exit_group(Errno::ENODEV as i32),
action => {
let _ = request.kill(action);
Err(Errno::ENODEV)
}
}
}
fn to_efd_flags(arg: u64) -> Result<EfdFlags, Errno> {
#[expect(clippy::cast_possible_truncation)]
let flags = arg as c_int;
EfdFlags::from_bits(flags).ok_or(Errno::EINVAL)
}