Skip to content
56 changes: 26 additions & 30 deletions crates/libcontainer/src/container/mount_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ fn container_userns_has_mappings(linux: Option<&Linux>) -> bool {
}
}

pub(crate) fn is_idmap_requested(mount: &SpecMount) -> bool {
let has_any_mount_mappings = mount.uid_mappings().is_some() || mount.gid_mappings().is_some();
let options = mount.options().as_deref().unwrap_or(&[]);
let has_idmap_option = options.iter().any(|o| o == "idmap" || o == "ridmap");
has_idmap_option || has_any_mount_mappings
}

fn validate_mount_mappings(mount: &SpecMount) -> Result<bool, ErrInvalidSpec> {
match (
mount.uid_mappings().as_deref(),
Expand Down Expand Up @@ -56,14 +63,11 @@ pub(crate) fn validate_idmapped_mounts(
let can_use_container_userns = container_userns_has_mappings(linux);

for mount in mounts {
let has_any_mount_mappings =
mount.uid_mappings().is_some() || mount.gid_mappings().is_some();
let options = mount.options().as_deref().unwrap_or(&[]);
let has_idmap_option = options.iter().any(|o| o == "idmap" || o == "ridmap");
if !has_idmap_option && !has_any_mount_mappings {
if !is_idmap_requested(mount) {
continue;
}

let options = mount.options().as_deref().unwrap_or(&[]);
let is_bind = mount.typ().as_deref() == Some("bind")
|| options.iter().any(|o| o == "bind" || o == "rbind");

Expand Down Expand Up @@ -92,13 +96,6 @@ pub(crate) fn validate_idmapped_mounts(
);
return Err(ErrInvalidSpec::MountIdmapMissingMappings);
}

// TODO: remove this guard when idmapped mount support is implemented.
tracing::error!(
destination = ?mount.destination(),
"idmapped mounts are not supported"
);
return Err(ErrInvalidSpec::MountIdmapUnsupported);
}

Ok(())
Expand Down Expand Up @@ -143,7 +140,7 @@ mod tests {
}

#[test]
fn validate_idmapped_mounts_rejects_idmap_with_mappings_bind_as_unsupported() {
fn validate_idmapped_mounts_allows_idmap_with_mappings_bind() {
let mount = base_mount()
.options(vec!["bind".to_string(), "idmap".to_string()])
.uid_mappings(vec![make_mapping()])
Expand All @@ -152,11 +149,11 @@ mod tests {
.unwrap();
let syscall = create_root_syscall();
let res = validate_idmapped_mounts(&[mount], None, rootless_required(&*syscall).unwrap());
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_mappings_without_idmap_flag_as_unsupported() {
fn validate_idmapped_mounts_allows_mappings_without_idmap_flag() {
let mount = base_mount()
.options(vec!["bind".to_string()])
.uid_mappings(vec![make_mapping()])
Expand All @@ -165,11 +162,11 @@ mod tests {
.unwrap();
let syscall = create_root_syscall();
let res = validate_idmapped_mounts(&[mount], None, rootless_required(&*syscall).unwrap());
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_implied_idmap_with_userns_as_unsupported() {
fn validate_idmapped_mounts_allows_implied_idmap_with_userns() {
let mount = base_mount()
.options(vec!["bind".to_string(), "idmap".to_string()])
.build()
Expand All @@ -191,11 +188,11 @@ mod tests {
Some(&linux),
rootless_required(&*syscall).unwrap(),
);
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_implied_idmap_with_joined_userns_as_unsupported() {
fn validate_idmapped_mounts_allows_implied_idmap_with_joined_userns() {
let mount = base_mount()
.options(vec!["bind".to_string(), "idmap".to_string()])
.build()
Expand All @@ -216,11 +213,11 @@ mod tests {
Some(&linux),
rootless_required(&*syscall).unwrap(),
);
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_ridmap_with_userns_as_unsupported() {
fn validate_idmapped_mounts_allows_ridmap_with_userns() {
let mount = base_mount()
.options(vec!["bind".to_string(), "ridmap".to_string()])
.build()
Expand All @@ -242,11 +239,11 @@ mod tests {
Some(&linux),
rootless_required(&*syscall).unwrap(),
);
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_rbind_idmap_as_unsupported() {
fn validate_idmapped_mounts_allows_rbind_idmap() {
let mount = MountBuilder::default()
.destination(PathBuf::from("/mnt"))
.source(PathBuf::from("/src"))
Expand All @@ -257,7 +254,7 @@ mod tests {
.unwrap();
let syscall = create_root_syscall();
let res = validate_idmapped_mounts(&[mount], None, rootless_required(&*syscall).unwrap());
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
Expand Down Expand Up @@ -436,8 +433,7 @@ mod tests {
}

#[test]
fn validate_idmapped_mounts_rejects_bind_type_without_bind_option_as_unsupported() {
// typ="bind" のみで is_bind を満たし、options に "bind"/"rbind" を含まないケース
fn validate_idmapped_mounts_allows_bind_type_without_bind_option() {
let mount = base_mount()
.options(vec!["idmap".to_string()])
.uid_mappings(vec![make_mapping()])
Expand All @@ -446,11 +442,11 @@ mod tests {
.unwrap();
let syscall = create_root_syscall();
let res = validate_idmapped_mounts(&[mount], None, rootless_required(&*syscall).unwrap());
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
fn validate_idmapped_mounts_rejects_unsupported_idmap_among_multiple_mounts() {
fn validate_idmapped_mounts_allows_idmap_among_multiple_mounts() {
let mapped_mount = base_mount()
.options(vec!["bind".to_string(), "idmap".to_string()])
.uid_mappings(vec![make_mapping()])
Expand All @@ -469,7 +465,7 @@ mod tests {
None,
rootless_required(&*syscall).unwrap(),
);
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(res.is_ok());
}

#[test]
Expand All @@ -495,7 +491,7 @@ mod tests {
None,
rootless_required(&*syscall).unwrap(),
);
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapUnsupported)));
assert!(matches!(res, Err(ErrInvalidSpec::MountIdmapNonBind)));
}

#[test]
Expand Down
2 changes: 0 additions & 2 deletions crates/libcontainer/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ pub enum ErrInvalidSpec {
MountIdmapNonBind,
#[error("idmapped mount requires uid/gid mappings or a usable user namespace")]
MountIdmapMissingMappings,
#[error("idmapped mount is not supported")]
MountIdmapUnsupported,
#[error("idmapped mounts are not supported in rootless containers")]
MountIdmapRootless,
#[error("invalid netns path: {0}")]
Expand Down
Loading
Loading