Skip to content

Commit

Permalink
Check if unprivileged user namespaces are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Furisto committed Nov 24, 2021
1 parent 64fd60d commit 4192841
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/libcontainer/src/rootless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{namespaces::Namespaces, utils};
use anyhow::{bail, Context, Result};
use nix::unistd::Pid;
use oci_spec::runtime::{Linux, LinuxIdMapping, LinuxNamespace, LinuxNamespaceType, Mount, Spec};
use std::fs;
use std::path::Path;
use std::process::Command;
use std::{env, path::PathBuf};
Expand Down Expand Up @@ -104,6 +105,22 @@ pub fn rootless_required() -> bool {
matches!(std::env::var("YOUKI_USE_ROOTLESS").as_deref(), Ok("true"))
}

pub fn unprivileged_user_ns_enabled() -> Result<bool> {
let user_ns_sysctl = Path::new("/proc/sys/kernel/unprivileged_userns_clone");
if !user_ns_sysctl.exists() {
return Ok(true);
}

let content =
fs::read_to_string(user_ns_sysctl).context("failed to read unprivileged userns clone")?;

match content.trim().parse::<u8>()? {
0 => Ok(false),
1 => Ok(true),
v => bail!("failed to parse unprivileged userns value: {}", v),
}
}

/// Validates that the spec contains the required information for
/// running in rootless mode
fn validate(spec: &Spec) -> Result<()> {
Expand Down
8 changes: 7 additions & 1 deletion crates/youki/src/commands/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::HashSet, fs, path::Path};

use anyhow::Result;
use clap::Parser;
use libcontainer::rootless;
use procfs::{CpuInfo, Meminfo};

use libcgroups::{common::CgroupSetup, v2::controller_type::ControllerType};
Expand Down Expand Up @@ -176,7 +177,12 @@ pub fn print_namespaces() {
println!(" {:<16}enabled", "mount");
print_feature_status(&content, "CONFIG_UTS_NS", FeatureDisplay::new("uts"));
print_feature_status(&content, "CONFIG_IPC_NS", FeatureDisplay::new("ipc"));
print_feature_status(&content, "CONFIG_USER_NS", FeatureDisplay::new("user"));

let user_display = match rootless::unprivileged_user_ns_enabled() {
Ok(false) => FeatureDisplay::with_status("user", "enabled (root only)", "disabled"),
_ => FeatureDisplay::new("user"),
};
print_feature_status(&content, "CONFIG_USER_NS", user_display);
print_feature_status(&content, "CONFIG_PID_NS", FeatureDisplay::new("pid"));
print_feature_status(&content, "CONFIG_NET_NS", FeatureDisplay::new("network"));
// While the CONFIG_CGROUP_NS kernel feature exists, it is obsolete and should not be used. CGroup namespaces
Expand Down

0 comments on commit 4192841

Please sign in to comment.