syd 3.58.0

rock-solid application kernel
Documentation
//
// Syd: rock-solid application kernel
// src/kernel/eventfd.rs: eventfd(2) and eventfd2(2) handlers
//
// Copyright (c) 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

// SAFETY: This module has been liberated from unsafe code!
#![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| {
        // Linux truncates upper bits.
        #[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;

        // Validate flags.
        let flags = to_efd_flags(req.data.args[1])?;

        // Linux truncates upper bits.
        #[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); // release the read lock.

    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() {
        // Sandboxing is off.
        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 => {
            // Stop|Kill
            let _ = request.kill(action);
            Err(Errno::ENODEV)
        }
    }
}

fn to_efd_flags(arg: u64) -> Result<EfdFlags, Errno> {
    // Linux truncates upper bits.
    #[expect(clippy::cast_possible_truncation)]
    let flags = arg as c_int;

    // Reject invalid bits.
    EfdFlags::from_bits(flags).ok_or(Errno::EINVAL)
}