Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions tests/contest/contest/src/tests/cgroups/devices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//! Checks the cgroupfs v2 device rules from inside the container with runtimetest's
//! `device_cgroup` check (block reads and writes). mknod is not checked: both runtimes append
//! `allow b *:* m` and `allow c *:* m`, and how a deny of it should behave is open in #3911.

use std::path::PathBuf;

use oci_spec::runtime::{LinuxDevice, LinuxDeviceCgroup, LinuxDeviceType};
use test_framework::{ConditionalTest, TestGroup, TestResult, test_result};

use crate::utils::test_utils::CreateOptions;
use crate::utils::{
block_node, create_spec_with_nodes, device_rule, is_cgroup_v2, test_inside_container,
};

/// runtimetest opens each node and checks it against `rules`.
fn run_device_test(
cgroup_name: &str,
rules: Vec<LinuxDeviceCgroup>,
nodes: Vec<LinuxDevice>,
) -> TestResult {
let spec = test_result!(create_spec_with_nodes(
PathBuf::from("/runtime-test").join(cgroup_name),
rules,
nodes,
vec!["runtimetest".into(), "device_cgroup".into()],
));
test_inside_container(&spec, &CreateOptions::default(), &|_| Ok(()))
}

fn test_device_default_deny() -> TestResult {
let rules = vec![test_result!(device_rule(false, None, None, None, "rwm"))];
let nodes = vec![test_result!(block_node(8, 0))];
run_device_test("devices_default_deny", rules, nodes)
}

fn test_device_allow_with_redundant_deny() -> TestResult {
let rules = vec![
test_result!(device_rule(false, None, None, None, "rwm")),
test_result!(device_rule(
true,
Some(LinuxDeviceType::B),
Some(8),
Some(0),
"rw"
)),
test_result!(device_rule(
false,
Some(LinuxDeviceType::B),
Some(8),
Some(1),
"rw"
)),
];
let nodes = vec![
test_result!(block_node(8, 0)),
test_result!(block_node(8, 1)),
test_result!(block_node(8, 2)),
];
run_device_test("devices_redundant_deny", rules, nodes)
}

fn test_device_deny_on_allow_all() -> TestResult {
let rules = vec![
test_result!(device_rule(true, None, None, None, "rwm")),
test_result!(device_rule(
false,
Some(LinuxDeviceType::B),
Some(8),
Some(0),
"rw"
)),
];
let nodes = vec![
test_result!(block_node(8, 0)),
test_result!(block_node(8, 1)),
];
run_device_test("devices_deny_on_allow_all", rules, nodes)
}

fn test_device_allow_then_deny_write() -> TestResult {
let rules = vec![
test_result!(device_rule(false, None, None, None, "rwm")),
test_result!(device_rule(
true,
Some(LinuxDeviceType::B),
Some(8),
Some(0),
"rw"
)),
test_result!(device_rule(
false,
Some(LinuxDeviceType::B),
Some(8),
Some(0),
"w"
)),
];
let nodes = vec![test_result!(block_node(8, 0))];
run_device_test("devices_allow_then_deny_write", rules, nodes)
}

fn can_run() -> bool {
nix::unistd::geteuid().is_root() && is_cgroup_v2()
}

pub fn get_test_group() -> TestGroup {
let mut test_group = TestGroup::new("cgroup_v2_devices");
let default_deny = ConditionalTest::new(
"device_default_deny",
Box::new(can_run),
Box::new(test_device_default_deny),
);
let redundant_deny = ConditionalTest::new(
"device_allow_with_redundant_deny",
Box::new(can_run),
Box::new(test_device_allow_with_redundant_deny),
);
let deny_on_allow_all = ConditionalTest::new(
"device_deny_on_allow_all",
Box::new(can_run),
Box::new(test_device_deny_on_allow_all),
);
let allow_then_deny_write = ConditionalTest::new(
"device_allow_then_deny_write",
Box::new(can_run),
Box::new(test_device_allow_then_deny_write),
);
test_group.add(vec![
Box::new(default_deny),
Box::new(redundant_deny),
Box::new(deny_on_allow_all),
Box::new(allow_then_deny_write),
]);
test_group
}

test_framework::register_test_group!(get_test_group);
1 change: 1 addition & 0 deletions tests/contest/contest/src/tests/cgroups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::Path;
use anyhow::{Context, Result};
pub mod cpu;
pub mod cpuset;
pub mod devices;
pub mod hugetlb;
pub mod io;
pub mod memory;
Expand Down
89 changes: 13 additions & 76 deletions tests/contest/contest/src/tests/cgroups/systemd/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,30 @@ use std::ffi::OsStr;
use std::path::PathBuf;

use anyhow::{Context, Result, bail};
use oci_spec::runtime::{
LinuxBuilder, LinuxDevice, LinuxDeviceBuilder, LinuxDeviceCgroup, LinuxDeviceCgroupBuilder,
LinuxDeviceType, LinuxResourcesBuilder, ProcessBuilder, Spec, SpecBuilder,
};
use oci_spec::runtime::{LinuxDevice, LinuxDeviceCgroup, LinuxDeviceType, Spec};
use test_framework::{ConditionalTest, TestGroup, TestResult, test_result};

use super::{SCOPE_PREFIX, can_run, show_property, unit_name};
use crate::utils::test_utils::{CreateOptions, check_container_created};
use crate::utils::{is_runtime_runc, test_inside_container, test_outside_container_with_options};
use crate::utils::{
block_node, create_spec_with_nodes, device_rule, is_runtime_runc, test_inside_container,
test_outside_container_with_options,
};

/// systemd cgroupsPath notation: [slice]:[scope prefix]:[name]
fn scope_path(cgroup_name: &str) -> PathBuf {
PathBuf::from(format!("system.slice:{SCOPE_PREFIX}:{cgroup_name}"))
}

fn create_spec(cgroup_name: &str, devices: Vec<LinuxDeviceCgroup>) -> Result<Spec> {
create_spec_with_nodes(
cgroup_name,
scope_path(cgroup_name),
devices,
vec![],
vec!["sleep".to_string(), "30".to_string()],
)
}

/// `nodes` are created in the container by the runtime; runtimetest opens them and checks
/// each against the device cgroup rules in `devices`.
fn create_spec_with_nodes(
cgroup_name: &str,
devices: Vec<LinuxDeviceCgroup>,
nodes: Vec<LinuxDevice>,
args: Vec<String>,
) -> Result<Spec> {
// systemd cgroupsPath notation: [slice]:[scope prefix]:[name]
let cgroups_path = PathBuf::from(format!("system.slice:{SCOPE_PREFIX}:{cgroup_name}"));

SpecBuilder::default()
.process(
ProcessBuilder::default()
.args(args)
.build()
.context("failed to build process spec")?,
)
.linux(
LinuxBuilder::default()
.cgroups_path(cgroups_path)
.devices(nodes)
.resources(
LinuxResourcesBuilder::default()
.devices(devices)
.build()
.context("failed to build resources spec")?,
)
.build()
.context("failed to build linux spec")?,
)
.build()
.context("failed to build spec")
}

/// A block device node for the runtime to create at `/dev/test-<major>-<minor>`.
fn block_node(major: i64, minor: i64) -> Result<LinuxDevice> {
LinuxDeviceBuilder::default()
.path(PathBuf::from(format!("/dev/test-{major}-{minor}")))
.typ(LinuxDeviceType::B)
.major(major)
.minor(minor)
.build()
.context("failed to build device node")
}

fn device_rule(
allow: bool,
typ: Option<LinuxDeviceType>,
major: Option<i64>,
minor: Option<i64>,
access: &str,
) -> Result<LinuxDeviceCgroup> {
let mut builder = LinuxDeviceCgroupBuilder::default()
.allow(allow)
.access(access);
if let Some(typ) = typ {
builder = builder.typ(typ);
}
if let Some(major) = major {
builder = builder.major(major);
}
if let Some(minor) = minor {
builder = builder.minor(minor);
}
builder.build().context("failed to build device rule")
}

fn device_policy(cgroup_name: &str) -> Result<String> {
show_property(&unit_name(cgroup_name), "DevicePolicy")
}
Expand Down Expand Up @@ -264,7 +201,7 @@ fn run_device_access_test(
nodes: Vec<LinuxDevice>,
) -> TestResult {
let spec = test_result!(create_spec_with_nodes(
cgroup_name,
scope_path(cgroup_name),
devices,
nodes,
vec!["runtimetest".to_string(), "device_cgroup".to_string()],
Expand Down Expand Up @@ -299,7 +236,7 @@ fn test_default_deny_is_enforced() -> TestResult {
));
let nodes = vec![test_result!(block_node(8, 0))];
let spec = test_result!(create_spec_with_nodes(
cgroup_name,
scope_path(cgroup_name),
vec![deny_all],
nodes,
vec!["runtimetest".to_string(), "device_cgroup".to_string()],
Expand Down
69 changes: 69 additions & 0 deletions tests/contest/contest/src/utils/cgroups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use libcgroups::common::{self, CgroupSetup};
use libcgroups::v2::controller_type::ControllerType;
use oci_spec::runtime::{
LinuxBuilder, LinuxDevice, LinuxDeviceBuilder, LinuxDeviceCgroup, LinuxDeviceCgroupBuilder,
LinuxDeviceType, LinuxResourcesBuilder, ProcessBuilder, Spec, SpecBuilder,
};
use tracing::debug;

use crate::utils::test_utils::CGROUP_ROOT;
Expand Down Expand Up @@ -106,3 +110,68 @@ pub fn block_device_numbers(count: usize) -> Result<Vec<(i64, i64)>> {

anyhow::bail!("needed {count} block devices with a dev file, found fewer")
}

/// A block device node for the runtime to create at `/dev/test-<major>-<minor>`.
pub fn block_node(major: i64, minor: i64) -> Result<LinuxDevice> {
LinuxDeviceBuilder::default()
.path(PathBuf::from(format!("/dev/test-{major}-{minor}")))
.typ(LinuxDeviceType::B)
.major(major)
.minor(minor)
.build()
.context("failed to build device node")
}

pub fn device_rule(
allow: bool,
typ: Option<LinuxDeviceType>,
major: Option<i64>,
minor: Option<i64>,
access: &str,
) -> Result<LinuxDeviceCgroup> {
let mut builder = LinuxDeviceCgroupBuilder::default()
.allow(allow)
.access(access);
if let Some(typ) = typ {
builder = builder.typ(typ);
}
if let Some(major) = major {
builder = builder.major(major);
}
if let Some(minor) = minor {
builder = builder.minor(minor);
}
builder.build().context("failed to build device rule")
}

/// `nodes` are created in the container by the runtime; runtimetest opens them and checks
/// each against the device cgroup rules in `devices`.
pub fn create_spec_with_nodes(
cgroups_path: PathBuf,
devices: Vec<LinuxDeviceCgroup>,
nodes: Vec<LinuxDevice>,
args: Vec<String>,
) -> Result<Spec> {
SpecBuilder::default()
.process(
ProcessBuilder::default()
.args(args)
.build()
.context("failed to build process spec")?,
)
.linux(
LinuxBuilder::default()
.cgroups_path(cgroups_path)
.devices(nodes)
.resources(
LinuxResourcesBuilder::default()
.devices(devices)
.build()
.context("failed to build resources spec")?,
)
.build()
.context("failed to build linux spec")?,
)
.build()
.context("failed to build spec")
}
4 changes: 2 additions & 2 deletions tests/contest/contest/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub mod support;
pub mod test_utils;

pub use cgroups::{
block_device_numbers, block_devices, cgroup_has_file, device_numbers, is_cgroup_v2,
is_cgroup_v2_with_controller,
block_device_numbers, block_devices, block_node, cgroup_has_file, create_spec_with_nodes,
device_numbers, device_rule, is_cgroup_v2, is_cgroup_v2_with_controller,
};
pub use support::{
generate_uuid, get_runtime_path, get_runtimetest_path, is_runtime_runc, is_runtime_youki,
Expand Down
Loading
Loading