Skip to content

Commit cdaf0d9

Browse files
committed
Drop cgroupv1 support
cgroupv1 is now completely removed from systemd after years of transition.
1 parent 3887f93 commit cdaf0d9

2 files changed

Lines changed: 20 additions & 104 deletions

File tree

src/cgroup.rs

Lines changed: 11 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use anyhow::{Context, Result, ensure};
1+
use anyhow::{Context, Result};
22
use aya::maps::{HashMap, MapData};
33
use aya::programs::{CgroupAttachMode, CgroupDevice, Link};
44
use std::ffi::OsStr;
@@ -23,87 +23,6 @@ bitflags::bitflags! {
2323
}
2424
}
2525

26-
pub trait DeviceAccessController {
27-
/// Set the permission for a specific device.
28-
fn set_permission(
29-
&mut self,
30-
ty: DeviceType,
31-
major: u32,
32-
minor: u32,
33-
access: Access,
34-
) -> Result<()>;
35-
}
36-
37-
pub struct DeviceAccessControllerV1 {
38-
cgroup: PathBuf,
39-
}
40-
41-
impl DeviceAccessControllerV1 {
42-
pub fn new(cgroup: &Path) -> Result<Self> {
43-
ensure!(
44-
cgroup.is_dir(),
45-
"cgroup {} does not exist",
46-
cgroup.display()
47-
);
48-
49-
Ok(Self {
50-
cgroup: cgroup.to_owned(),
51-
})
52-
}
53-
}
54-
55-
impl DeviceAccessController for DeviceAccessControllerV1 {
56-
fn set_permission(
57-
&mut self,
58-
ty: DeviceType,
59-
major: u32,
60-
minor: u32,
61-
access: Access,
62-
) -> Result<()> {
63-
let mut denied = String::with_capacity(3);
64-
let mut allowed = String::with_capacity(3);
65-
66-
let ty = match ty {
67-
DeviceType::Character => 'c',
68-
DeviceType::Block => 'b',
69-
};
70-
71-
if access.contains(Access::READ) {
72-
allowed.push('r');
73-
} else {
74-
denied.push('r');
75-
}
76-
77-
if access.contains(Access::WRITE) {
78-
allowed.push('w');
79-
} else {
80-
denied.push('w');
81-
}
82-
83-
if access.contains(Access::MKNOD) {
84-
allowed.push('m');
85-
} else {
86-
denied.push('m');
87-
}
88-
89-
if !denied.is_empty() {
90-
std::fs::write(
91-
self.cgroup.join("devices.deny"),
92-
format!("{ty} {major}:{minor} {denied}"),
93-
)?;
94-
}
95-
96-
if !allowed.is_empty() {
97-
std::fs::write(
98-
self.cgroup.join("devices.allow"),
99-
format!("{ty} {major}:{minor} {allowed}"),
100-
)?;
101-
}
102-
103-
Ok(())
104-
}
105-
}
106-
10726
#[repr(C)] // This is read as POD by the BPF program.
10827
#[derive(Clone, Copy)]
10928
struct Device {
@@ -115,12 +34,18 @@ struct Device {
11534
// SAFETY: Device is `repr(C)`` and has no padding.
11635
unsafe impl aya::Pod for Device {}
11736

118-
pub struct DeviceAccessControllerV2 {
37+
pub struct DeviceAccessController {
11938
map: HashMap<MapData, Device, u32>,
12039
pin: PathBuf,
12140
}
12241

123-
impl DeviceAccessControllerV2 {
42+
impl Drop for DeviceAccessController {
43+
fn drop(&mut self) {
44+
let _ = std::fs::remove_file(&self.pin);
45+
}
46+
}
47+
48+
impl DeviceAccessController {
12449
pub fn new(cgroup: &Path) -> Result<Self> {
12550
// cgroup is of form "/sys/fs/cgroup/system.slice/xxx-yyy.scope", and we can use
12651
// the last part as unique identifier.
@@ -174,16 +99,9 @@ impl DeviceAccessControllerV2 {
17499

175100
Ok(Self { map, pin })
176101
}
177-
}
178102

179-
impl Drop for DeviceAccessControllerV2 {
180-
fn drop(&mut self) {
181-
let _ = std::fs::remove_file(&self.pin);
182-
}
183-
}
184-
185-
impl DeviceAccessController for DeviceAccessControllerV2 {
186-
fn set_permission(
103+
/// Set the permission for a specific device.
104+
pub fn set_permission(
187105
&mut self,
188106
ty: DeviceType,
189107
major: u32,

src/runc/container.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use tokio::io::Interest;
1212
use tokio::io::unix::AsyncFd;
1313
use tokio::sync::Mutex;
1414

15-
use crate::cgroup::{
16-
Access, DeviceAccessController, DeviceAccessControllerV1, DeviceAccessControllerV2, DeviceType,
17-
};
15+
use crate::cgroup::{Access, DeviceAccessController, DeviceType};
1816

1917
struct CgroupEventNotifier {
2018
file: AsyncFd<File>,
@@ -72,7 +70,7 @@ pub struct Container {
7270
gid: u32,
7371
pid: Pid,
7472
wait: tokio::sync::watch::Receiver<bool>,
75-
cgroup_device_filter: Mutex<Box<dyn DeviceAccessController + Send>>,
73+
cgroup_device_filter: Mutex<DeviceAccessController>,
7674
}
7775

7876
impl Container {
@@ -105,12 +103,12 @@ impl Container {
105103
"/run/systemd/transient/{cgroup_name}.d/50-DevicePolicy.conf"
106104
));
107105

108-
let cgroup_device_filter: Box<dyn DeviceAccessController + Send> =
109-
if let Some(device_cgroup) = &state.cgroup_paths.devices {
110-
Box::new(DeviceAccessControllerV1::new(device_cgroup)?)
111-
} else {
112-
Box::new(DeviceAccessControllerV2::new(&state.cgroup_paths.unified)?)
113-
};
106+
anyhow::ensure!(
107+
state.cgroup_paths.devices.is_none(),
108+
"cgroupv1 is no longer supported"
109+
);
110+
111+
let cgroup_device_filter = DeviceAccessController::new(&state.cgroup_paths.unified)?;
114112

115113
let container = Self {
116114
uid: config.process.user.uid,
@@ -211,7 +209,7 @@ impl Container {
211209
// The old file might be a bind mount. Try umount it.
212210
let _ = rustix::mount::unmount(file.path(), UnmountFlags::DETACH);
213211
} else {
214-
anyhow::bail!("Unknown file present in /dev");
212+
bail!("Unknown file present in /dev");
215213
}
216214
}
217215

0 commit comments

Comments
 (0)