Skip to main content

tilo_process/
confinement.rs

1//! Whether this process could build the user and mount namespace a confined guest needs:
2//! not under a seccomp filter, with unprivileged user namespaces allowed.
3
4/// Whether a seccomp filter confines this process.
5#[must_use]
6pub fn seccomp_filtered() -> bool {
7    std::fs::read_to_string("/proc/self/status")
8        .ok()
9        .and_then(|status| {
10            status.lines().find_map(|line| {
11                line.strip_prefix("Seccomp:")
12                    .map(|value| value.trim().to_owned())
13            })
14        })
15        .and_then(|value| value.parse::<u32>().ok())
16        .is_some_and(|mode| mode >= 2)
17}
18
19/// Whether the kernel allows unprivileged user namespaces. An absent sysctl means the kernel
20/// default, so only an explicit disable reports false.
21#[must_use]
22pub fn unprivileged_userns_enabled() -> bool {
23    // Debian/Ubuntu knob: `0` disables unprivileged user namespaces entirely.
24    if let Ok(value) = std::fs::read_to_string("/proc/sys/kernel/unprivileged_userns_clone")
25        && value.trim() == "0"
26    {
27        return false;
28    }
29    // Upstream cap on how many user namespaces may exist: `0` means none.
30    if let Ok(value) = std::fs::read_to_string("/proc/sys/user/max_user_namespaces")
31        && value.trim() == "0"
32    {
33        return false;
34    }
35    true
36}