Skip to content

Commit 6552032

Browse files
committed
Use ManuallyDrop instead of stop function for cgroup lifecycle.
1 parent 09cbf0f commit 6552032

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/cgroup.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ pub trait DeviceAccessController {
3131
minor: u32,
3232
access: Access,
3333
) -> Result<()>;
34-
35-
/// Stop performing access control. This may allow all accesses, so should only be used when
36-
/// the cgroup is shutdown.
37-
fn stop(self: Box<Self>) -> Result<()>;
3834
}
3935

4036
pub struct DeviceAccessControllerV1 {
@@ -105,10 +101,6 @@ impl DeviceAccessController for DeviceAccessControllerV1 {
105101

106102
Ok(())
107103
}
108-
109-
fn stop(self: Box<Self>) -> Result<()> {
110-
Ok(())
111-
}
112104
}
113105

114106
#[repr(C)] // This is read as POD by the BPF program.
@@ -179,6 +171,12 @@ impl DeviceAccessControllerV2 {
179171
}
180172
}
181173

174+
impl Drop for DeviceAccessControllerV2 {
175+
fn drop(&mut self) {
176+
let _ = std::fs::remove_file(&self.pin);
177+
}
178+
}
179+
182180
impl DeviceAccessController for DeviceAccessControllerV2 {
183181
fn set_permission(
184182
&mut self,
@@ -199,11 +197,6 @@ impl DeviceAccessController for DeviceAccessControllerV2 {
199197
}
200198
Ok(())
201199
}
202-
203-
fn stop(self: Box<Self>) -> Result<()> {
204-
CgroupDevice::from_pin(&self.pin)?.unpin()?;
205-
Ok(())
206-
}
207200
}
208201

209202
pub struct DeviceAccessControllerDummy;
@@ -218,8 +211,4 @@ impl DeviceAccessController for DeviceAccessControllerDummy {
218211
) -> Result<()> {
219212
bail!("neither cgroup v1 and cgroup v2 works");
220213
}
221-
222-
fn stop(self: Box<Self>) -> Result<()> {
223-
Ok(())
224-
}
225214
}

src/docker/container.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::mem::ManuallyDrop;
12
use std::pin::pin;
23
use std::sync::{Arc, Mutex};
34
use std::time::Duration;
@@ -22,7 +23,7 @@ pub struct Container {
2223
id: String,
2324
user: String,
2425
remove_event: Shared<BoxFuture<'static, Option<EventMessage>>>,
25-
cgroup_device_filter: Arc<Mutex<Option<Box<dyn DeviceAccessController + Send>>>>,
26+
cgroup_device_filter: Arc<Mutex<Option<ManuallyDrop<Box<dyn DeviceAccessController + Send>>>>>,
2627
}
2728

2829
impl Container {
@@ -61,6 +62,10 @@ impl Container {
6162
},
6263
};
6364

65+
// Dropping the device filter will cause the container to have arbitrary device access.
66+
// So keep it alive until we're sure that the container is stopped.
67+
let cgroup_device_filter = ManuallyDrop::new(cgroup_device_filter);
68+
6469
Ok(Self {
6570
docker: docker.clone(),
6671
id,
@@ -109,12 +114,9 @@ impl Container {
109114
}
110115

111116
// Stop the cgroup device filter. Only do so once we're sure that the container is removed.
112-
self.cgroup_device_filter
113-
.lock()
114-
.unwrap()
115-
.take()
116-
.unwrap()
117-
.stop()?;
117+
drop(ManuallyDrop::into_inner(
118+
self.cgroup_device_filter.lock().unwrap().take().unwrap(),
119+
));
118120

119121
Ok(())
120122
}

0 commit comments

Comments
 (0)