Skip to content
Closed
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
54 changes: 45 additions & 9 deletions crates/libcontainer/src/rootfs/mount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ impl Mount {
})?;
}
_ => {
if mount.destination() == Path::new("/dev") {
self.check_proc_mount(options.root, mount)?;

if Path::new(mount.destination()).normalize() == Path::new("/dev") {
mount_option_config.flags &= !MsFlags::MS_RDONLY;
Comment on lines +225 to 226
self.mount_into_container(
mount,
Expand Down Expand Up @@ -1047,14 +1049,16 @@ impl Mount {
let dest = mount.destination();

let container_proc_path = rootfs.join("proc");
let dest_path = rootfs.join_safely(dest).map_err(|err| {
tracing::error!(
"could not join rootfs path with mount destination {:?}: {}",
dest,
err
);
MountError::Other(err.into())
})?;
let dest_path = rootfs
.join_safely(Path::new(dest).normalize())
.map_err(|err| {
tracing::error!(
"could not join rootfs path with mount destination {:?}: {}",
dest,
err
);
MountError::Other(err.into())
})?;

// If path is Ok, it means dest_path is under /proc.
// - Ok(p) with p.is_empty(): mount target is exactly /proc.
Expand Down Expand Up @@ -1903,4 +1907,36 @@ mod tests {

Ok(())
}

#[test]
fn setup_mount_non_proc_fails_on_denied_proc_destination() -> Result<()> {
let tmp = tempfile::tempdir()?;
let rootfs = tmp.path();
let custom = rootfs.join("proc/custom");
fs::create_dir_all(custom.parent().unwrap())?;

let options = MountOptions {
root: rootfs,
label: None,
cgroup_ns: true,
};
let m = Mount::new();

let bind_mount = SpecMountBuilder::default()
.destination(PathBuf::from("/proc/custom"))
.typ("bind")
.source(&custom)
.options(vec!["bind".to_string()])
.build()?;
assert!(m.setup_mount(&bind_mount, &options).is_err());

let unnormalized_mount = SpecMountBuilder::default()
.destination(PathBuf::from("/tmp/../proc/custom"))
.typ("tmpfs")
.source(PathBuf::from("tmpfs"))
.build()?;
assert!(m.setup_mount(&unnormalized_mount, &options).is_err());
Comment on lines +1928 to +1938

Ok(())
}
}