oscall 0.1.1

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! `process::linux::landlock`: the three system calls of Landlock.
//!
//! The constants and layouts are those of `include/uapi/linux/landlock.h`,
//! which every architecture shares. The bits of the access rights are the
//! kernel's: `AccessFs`, `AccessNet` and `Scope` are passed as they are.

use crate::process::linux::landlock::{AccessFs, AccessNet, RestrictFlags, Scope};
use crate::sys::syscall;
use crate::{BorrowedHandle, OwnedHandle, Result};

const LANDLOCK_CREATE_RULESET_VERSION: usize = 1;
const LANDLOCK_CREATE_RULESET_ERRATA: usize = 1 << 1;

const LANDLOCK_RULE_PATH_BENEATH: usize = 1;
const LANDLOCK_RULE_NET_PORT: usize = 2;

/// `struct landlock_ruleset_attr`. It grew: the first version of Landlock
/// has the first member alone, the fourth the second, the sixth the third.
#[repr(C)]
struct RulesetAttr {
    handled_access_fs: u64,
    handled_access_net: u64,
    scoped: u64,
}

/// `struct landlock_path_beneath_attr`, which is packed: twelve bytes.
#[repr(C, packed)]
struct PathBeneathAttr {
    allowed_access: u64,
    parent_fd: i32,
}

/// `struct landlock_net_port_attr`.
#[repr(C)]
struct NetPortAttr {
    allowed_access: u64,
    port: u64,
}

const _: () = assert!(
    size_of::<RulesetAttr>() == 24
        && size_of::<PathBeneathAttr>() == 12
        && size_of::<NetPortAttr>() == 16
);

/// `landlock_create_ruleset` with no attribute: a number about the
/// kernel.
fn probe(flag: usize) -> Result<u32> {
    // SAFETY: no memory is involved.
    unsafe { syscall!(LANDLOCK_CREATE_RULESET, 0usize, 0usize, flag) }.map(|value| value as u32)
}

pub(crate) fn abi_version() -> Result<u32> {
    probe(LANDLOCK_CREATE_RULESET_VERSION)
}

pub(crate) fn errata() -> Result<u32> {
    probe(LANDLOCK_CREATE_RULESET_ERRATA)
}

pub(crate) fn create_ruleset(fs: AccessFs, net: AccessNet, scopes: Scope) -> Result<OwnedHandle> {
    let attr = RulesetAttr {
        handled_access_fs: fs.bits(),
        handled_access_net: net.bits(),
        scoped: scopes.bits(),
    };
    // A kernel takes a structure longer than the one it knows when what
    // it does not know is zero, and one shorter than its own as if the
    // rest were. The shortest that says everything is the one an older
    // kernel is the most likely to know.
    let size = if !scopes.is_empty() {
        24
    } else if !net.is_empty() {
        16
    } else {
        8usize
    };
    // SAFETY: the kernel reads `size` bytes of the structure.
    let fd = unsafe { syscall!(LANDLOCK_CREATE_RULESET, &raw const attr, size, 0usize) }?;
    // SAFETY: the descriptor was just created (close-on-exec, as those of
    // rulesets always are).
    Ok(unsafe { OwnedHandle::from_raw(fd as i32) })
}

pub(crate) fn add_path_rule(
    ruleset: BorrowedHandle<'_>,
    parent: BorrowedHandle<'_>,
    allowed: AccessFs,
) -> Result<()> {
    let attr = PathBeneathAttr {
        allowed_access: allowed.bits(),
        parent_fd: parent.as_raw(),
    };
    let (ruleset, kind) = (ruleset.as_raw(), LANDLOCK_RULE_PATH_BENEATH);
    // SAFETY: the kernel reads one `struct landlock_path_beneath_attr`,
    // at any alignment.
    unsafe { syscall!(LANDLOCK_ADD_RULE, ruleset, kind, &raw const attr, 0usize) }.map(drop)
}

pub(crate) fn add_port_rule(
    ruleset: BorrowedHandle<'_>,
    port: u16,
    allowed: AccessNet,
) -> Result<()> {
    let attr = NetPortAttr {
        allowed_access: allowed.bits(),
        port: u64::from(port),
    };
    let (ruleset, kind) = (ruleset.as_raw(), LANDLOCK_RULE_NET_PORT);
    // SAFETY: the kernel reads one `struct landlock_net_port_attr`.
    unsafe { syscall!(LANDLOCK_ADD_RULE, ruleset, kind, &raw const attr, 0usize) }.map(drop)
}

/// `landlock_restrict_self`. Only a system call: the child of `spawn`
/// makes it.
pub(crate) fn restrict_self_raw(ruleset: i32, flags: u32) -> Result<()> {
    // SAFETY: no memory is involved.
    unsafe { syscall!(LANDLOCK_RESTRICT_SELF, ruleset, flags) }.map(drop)
}

pub(crate) fn restrict_self(ruleset: BorrowedHandle<'_>, flags: RestrictFlags) -> Result<()> {
    restrict_self_raw(ruleset.as_raw(), flags.bits())
}