From ccb5046afcf4e4fd92b85c1da1b583b302f5dc83 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Tue, 13 Oct 2026 20:31:50 +0900 Subject: [PATCH 1/2] fix(libcontainer): check proc-safety on the resolved mountpoint Signed-off-by: Yusuke Sakurai --- crates/libcontainer/src/rootfs/mount.rs | 225 +++++++++++++++--- .../prohibit_symlink/prohibit_symlink_test.rs | 121 ++++++++-- 2 files changed, 302 insertions(+), 44 deletions(-) diff --git a/crates/libcontainer/src/rootfs/mount.rs b/crates/libcontainer/src/rootfs/mount.rs index dc645c14ba..eabdec9d81 100644 --- a/crates/libcontainer/src/rootfs/mount.rs +++ b/crates/libcontainer/src/rootfs/mount.rs @@ -211,7 +211,7 @@ impl Mount { _ => {} } - self.check_proc_mount(options.root, mount)?; + self.check_proc_mount(options.root, &dest_path, mount)?; self.mount_into_container(mount, options.root, &mount_option_config, options.label) .map_err(|err| { @@ -642,6 +642,32 @@ impl Mount { let dest: OwnedFd = root.resolve(container_dest)?.into(); let dest_fd = dest.as_fd(); + let procfs = ProcfsHandle::new()?; + let real_path = |fd: BorrowedFd| { + procfs.readlink(ProcfsBase::ProcThreadSelf, format!("fd/{}", fd.as_raw_fd())) + }; + let real_rootfs = real_path(root.as_fd())?; + let real_dest = real_path(dest_fd)?; + if !real_dest.starts_with(&real_rootfs) { + return Err(MountError::Custom(format!( + "mountpoint {real_dest:?} is outside of rootfs {real_rootfs:?}" + ))); + } + if real_dest == real_rootfs { + return Err(MountError::Custom(format!( + "mountpoint {real_dest:?} is on the top of rootfs {real_rootfs:?}" + ))); + } + self.check_proc_mount(&real_rootfs, &real_dest, m) + .inspect_err(|err| { + tracing::error!( + ?err, + destination = ?container_dest, + resolved = ?real_dest, + "proc-safety check failed" + ); + })?; + // fd-based mount flow: // - bind: open_tree -> mount_setattr -> move_mount // - nonbind: fsopen -> fsconfig -> fsmount -> mount_setattr -> move_mount @@ -757,10 +783,10 @@ impl Mount { } tracing::debug!(?err, "fsconfig failed, falling back to mount(2)"); return self.legacy_mount( + m, source, &root, - container_dest, - typ, + dest_fd, mount_option_config, &data_options, ); @@ -853,10 +879,10 @@ impl Mount { fn legacy_mount( &self, + m: &SpecMount, source: &Path, root: &Root, - container_dest: &Path, - typ: Option<&str>, + dest_fd: BorrowedFd, mount_option_config: &MountOptionConfig, data_options: &[String], ) -> Result<()> { @@ -865,17 +891,16 @@ impl Mount { .map(String::as_str) .filter(|s| !s.is_empty()) .collect(); - let dest: OwnedFd = root.resolve(container_dest)?.into(); self.syscall.mount( Some(source), - Path::new(&format!("/proc/self/fd/{}", dest.as_raw_fd())), - typ, + Path::new(&format!("/proc/self/fd/{}", dest_fd.as_raw_fd())), + m.typ().as_deref(), mount_option_config.flags, Some(&data.join(",")), )?; if let Some(rec_attr) = &mount_option_config.rec_attr { - let mounted: OwnedFd = root.resolve(container_dest)?.into(); + let mounted: OwnedFd = root.resolve(m.destination())?.into(); self.syscall.mount_setattr( mounted.as_fd(), Path::new(""), @@ -983,7 +1008,8 @@ impl Mount { } /// check_proc_mount checks to ensure that the mount destination is not over the top of /proc. - /// dest is required to be an abs path and have any symlinks resolved before calling this function. + /// dest_path is the host path of the mount destination under rootfs. It should have any + /// symlinks resolved, otherwise a symlink in the rootfs can point the mount at /proc. /// # Example (a valid case where `/proc` is mounted with `proc` type.) /// /// ``` @@ -1005,7 +1031,8 @@ impl Mount { /// .build() /// .expect("failed to build SpecMount"); /// - /// assert!(mounter.check_proc_mount(rootfs.as_path(), &mount).is_ok()); + /// let dest_path = rootfs.join("proc"); + /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount).is_ok()); /// ``` /// # Example (bind mount to `/proc` that should fail) /// ``` @@ -1027,9 +1054,15 @@ impl Mount { /// .build() /// .expect("failed to build SpecMount"); /// - /// assert!(mounter.check_proc_mount(rootfs.as_path(), &mount).is_err()); + /// let dest_path = rootfs.join("proc"); + /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount).is_err()); /// ``` - pub fn check_proc_mount(&self, rootfs: &Path, mount: &SpecMount) -> Result<()> { + pub fn check_proc_mount( + &self, + rootfs: &Path, + dest_path: &Path, + mount: &SpecMount, + ) -> Result<()> { const PROC_ROOT_INO: u64 = 1; const VALID_PROC_MOUNTS: &[&str] = &[ "/proc/cpuinfo", @@ -1045,16 +1078,7 @@ 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()) - })?; // If path is Ok, it means dest_path is under /proc. // - Ok(p) with p.is_empty(): mount target is exactly /proc. @@ -1796,7 +1820,12 @@ mod tests { .source(PathBuf::from("proc")) .build()?; - assert!(mounter.check_proc_mount(rootfs.path(), &mount).is_ok()); + let dest_path = rootfs.path().join("proc"); + assert!( + mounter + .check_proc_mount(rootfs.path(), &dest_path, &mount) + .is_ok() + ); Ok(()) } @@ -1810,10 +1839,14 @@ mod tests { let mount = SpecMountBuilder::default() .destination(PathBuf::from("/proc/uptime")) .typ("bind".to_string()) - .source(uptime) + .source(uptime.clone()) .build()?; - assert!(mounter.check_proc_mount(rootfs.path(), &mount).is_ok()); + assert!( + mounter + .check_proc_mount(rootfs.path(), &uptime, &mount) + .is_ok() + ); Ok(()) } @@ -1827,10 +1860,14 @@ mod tests { let mount = SpecMountBuilder::default() .destination(PathBuf::from("/proc/custom")) .typ("bind".to_string()) - .source(custom) + .source(custom.clone()) .build()?; - assert!(mounter.check_proc_mount(rootfs.path(), &mount).is_err()); + assert!( + mounter + .check_proc_mount(rootfs.path(), &custom, &mount) + .is_err() + ); Ok(()) } @@ -1903,4 +1940,138 @@ mod tests { Ok(()) } + + #[test] + fn setup_mount_bind_fails_if_destination_resolves_to_proc() -> Result<()> { + let tmp = tempfile::tempdir()?; + let rootfs = tmp.path(); + let src = tempfile::tempdir()?; + + fs::create_dir_all(rootfs.join("proc"))?; + symlink("proc", rootfs.join("data"))?; + + let mount = SpecMountBuilder::default() + .destination(PathBuf::from("/data")) + .typ("bind") + .source(src.path()) + .options(vec!["bind".to_string()]) + .build()?; + + let options = MountOptions { + root: rootfs, + label: None, + cgroup_ns: true, + }; + + let m = Mount::new(); + let res = m.setup_mount(&mount, &options); + + // /data resolves to /proc, so it must be checked like a mount on /proc + assert!(res.is_err()); + let err = format!("{:?}", res.err().unwrap()); + assert!(err.contains("cannot be mounted because it is not type proc")); + assert!(helper_syscall(&m).get_move_mount_args().is_empty()); + + Ok(()) + } + + #[test] + fn setup_mount_bind_fails_if_destination_resolves_under_proc() -> Result<()> { + let tmp = tempfile::tempdir()?; + let rootfs = tmp.path(); + let src = tempfile::tempdir()?; + + fs::create_dir_all(rootfs.join("proc/sys"))?; + symlink("proc/sys", rootfs.join("data"))?; + + let mount = SpecMountBuilder::default() + .destination(PathBuf::from("/data")) + .typ("bind") + .source(src.path()) + .options(vec!["bind".to_string()]) + .build()?; + + let options = MountOptions { + root: rootfs, + label: None, + cgroup_ns: true, + }; + + let m = Mount::new(); + let res = m.setup_mount(&mount, &options); + + assert!(res.is_err()); + let err = format!("{:?}", res.err().unwrap()); + assert!(err.contains("is not a valid mount under /proc")); + assert!(helper_syscall(&m).get_move_mount_args().is_empty()); + + Ok(()) + } + + #[test] + fn setup_mount_bind_to_symlink_outside_proc_is_allowed() -> Result<()> { + let tmp = tempfile::tempdir()?; + let rootfs = tmp.path(); + let src = tempfile::tempdir()?; + + fs::create_dir_all(rootfs.join("real"))?; + symlink("real", rootfs.join("data"))?; + + let mount = SpecMountBuilder::default() + .destination(PathBuf::from("/data")) + .typ("bind") + .source(src.path()) + .options(vec!["bind".to_string()]) + .build()?; + + let options = MountOptions { + root: rootfs, + label: None, + cgroup_ns: true, + }; + + let m = Mount::new(); + m.setup_mount(&mount, &options)?; + + let mv = helper_syscall(&m).get_move_mount_args(); + assert_eq!(mv.len(), 1); + assert_eq!( + mv[0].to_dirfd_path.as_deref(), + Some(fs::canonicalize(rootfs.join("real"))?.as_path()) + ); + + Ok(()) + } + + #[test] + fn setup_mount_bind_fails_if_destination_resolves_to_rootfs() -> Result<()> { + let tmp = tempfile::tempdir()?; + let rootfs = tmp.path(); + let src = tempfile::tempdir()?; + + symlink("/", rootfs.join("data"))?; + + let mount = SpecMountBuilder::default() + .destination(PathBuf::from("/data")) + .typ("bind") + .source(src.path()) + .options(vec!["bind".to_string()]) + .build()?; + + let options = MountOptions { + root: rootfs, + label: None, + cgroup_ns: true, + }; + + let m = Mount::new(); + let res = m.setup_mount(&mount, &options); + + assert!(res.is_err()); + let err = format!("{:?}", res.err().unwrap()); + assert!(err.contains("is on the top of rootfs")); + assert!(helper_syscall(&m).get_move_mount_args().is_empty()); + + Ok(()) + } } diff --git a/tests/contest/contest/src/tests/prohibit_symlink/prohibit_symlink_test.rs b/tests/contest/contest/src/tests/prohibit_symlink/prohibit_symlink_test.rs index 3f73a87d28..f524668e65 100644 --- a/tests/contest/contest/src/tests/prohibit_symlink/prohibit_symlink_test.rs +++ b/tests/contest/contest/src/tests/prohibit_symlink/prohibit_symlink_test.rs @@ -1,8 +1,10 @@ use std::fs; use std::os::unix::fs::symlink; +use std::path::PathBuf; use anyhow::{Context, Ok, Result, anyhow}; -use oci_spec::runtime::{ProcessBuilder, Spec, SpecBuilder}; +use oci_spec::runtime::{MountBuilder, ProcessBuilder, Spec, SpecBuilder, get_default_mounts}; +use tempfile::TempDir; use test_framework::{Test, TestGroup, TestResult, test_result}; use crate::utils::test_inside_container; @@ -22,6 +24,25 @@ fn create_spec() -> Result { Ok(spec) } +fn check_create_error(result: TestResult, expected_messages: &[&str]) -> TestResult { + match result { + TestResult::Failed(e) => { + let err_str = format!("{:?}", e); + if expected_messages.iter().any(|m| err_str.contains(m)) { + TestResult::Passed + } else { + TestResult::Failed(anyhow!( + "unexpected error (expected substring not found): {err_str}" + )) + } + } + TestResult::Skipped(_) => TestResult::Failed(anyhow!("test was skipped unexpectedly.")), + TestResult::Passed => { + TestResult::Failed(anyhow!("container creation succeeded unexpectedly.")) + } + } +} + fn prohibit_symlink_test(path: String) -> TestResult { let spec = test_result!(create_spec()); let result = test_inside_container(&spec, &CreateOptions::default(), &|bundle| { @@ -45,22 +66,45 @@ fn prohibit_symlink_test(path: String) -> TestResult { Ok(()) }); - match result { - TestResult::Failed(e) => { - let err_str = format!("{:?}", e); - if err_str.contains("must be mounted on ordinary directory") { - TestResult::Passed - } else { - TestResult::Failed(anyhow!( - "unexpected error (expected substring not found): {err_str}" - )) - } - } - TestResult::Skipped(_) => TestResult::Failed(anyhow!("test was skipped unexpectedly.")), - TestResult::Passed => { - TestResult::Failed(anyhow!("container creation succeeded unexpectedly.")) - } - } + check_create_error(result, &["must be mounted on ordinary directory"]) +} + +// Bind mount a host directory on /data, where /data in the rootfs is a symlink to `target`. +// The mount must be checked against the path the symlink resolves to, not /data. +fn prohibit_symlink_bind_test(target: &str, expected_messages: &[&str]) -> TestResult { + let source = test_result!(TempDir::new().context("failed to create bind source")); + + let mut mounts = get_default_mounts(); + mounts.push(test_result!( + MountBuilder::default() + .destination(PathBuf::from("/data")) + .typ("bind") + .source(source.path()) + .options(vec!["bind".to_string()]) + .build() + .context("failed to build bind mount") + )); + + let process = test_result!( + ProcessBuilder::default() + .args(vec!["true".to_string()]) + .build() + .context("failed to build process") + ); + let spec = test_result!( + SpecBuilder::default() + .process(process) + .mounts(mounts) + .build() + .context("failed to build spec") + ); + + let result = test_inside_container(&spec, &CreateOptions::default(), &|rootfs| { + symlink(target, rootfs.join("data"))?; + Ok(()) + }); + + check_create_error(result, expected_messages) } fn prohibit_symlink_proc_test() -> TestResult { @@ -71,6 +115,34 @@ fn prohibit_symlink_sys_test() -> TestResult { prohibit_symlink_test("sys".to_string()) } +fn prohibit_symlink_bind_to_proc_test() -> TestResult { + prohibit_symlink_bind_test( + "proc", + &[ + // youki + "cannot be mounted because it is not type proc", + // runc + "cannot be mounted because it is not of type proc", + ], + ) +} + +fn prohibit_symlink_bind_under_proc_test() -> TestResult { + prohibit_symlink_bind_test( + "proc/sys", + &[ + // youki + "is not a valid mount under /proc", + // runc + "cannot be mounted because it is inside /proc", + ], + ) +} + +fn prohibit_symlink_bind_to_rootfs_test() -> TestResult { + prohibit_symlink_bind_test("/", &["is on the top of rootfs"]) +} + pub fn get_prohibit_symlink_test() -> TestGroup { let mut prohibit_symlink_test_group = TestGroup::new("prohibit_symlink"); @@ -82,9 +154,24 @@ pub fn get_prohibit_symlink_test() -> TestGroup { "prohibit_symlink_sys_test", Box::new(prohibit_symlink_sys_test), ); + let prohibit_symlink_bind_to_proc_test = Test::new( + "prohibit_symlink_bind_to_proc_test", + Box::new(prohibit_symlink_bind_to_proc_test), + ); + let prohibit_symlink_bind_under_proc_test = Test::new( + "prohibit_symlink_bind_under_proc_test", + Box::new(prohibit_symlink_bind_under_proc_test), + ); + let prohibit_symlink_bind_to_rootfs_test = Test::new( + "prohibit_symlink_bind_to_rootfs_test", + Box::new(prohibit_symlink_bind_to_rootfs_test), + ); prohibit_symlink_test_group.add(vec![ Box::new(prohibit_symlink_proc_test), Box::new(prohibit_symlink_sys_test), + Box::new(prohibit_symlink_bind_to_proc_test), + Box::new(prohibit_symlink_bind_under_proc_test), + Box::new(prohibit_symlink_bind_to_rootfs_test), ]); prohibit_symlink_test_group From 7f9f82f0c3641c646bb86d1c792856870f5e4190 Mon Sep 17 00:00:00 2001 From: Yusuke Sakurai Date: Sun, 25 Oct 2026 13:26:07 +0900 Subject: [PATCH 2/2] address for review Signed-off-by: Yusuke Sakurai --- crates/libcontainer/src/rootfs/mount.rs | 86 +++++++++++++++++-------- 1 file changed, 60 insertions(+), 26 deletions(-) diff --git a/crates/libcontainer/src/rootfs/mount.rs b/crates/libcontainer/src/rootfs/mount.rs index eabdec9d81..b89ec71fc4 100644 --- a/crates/libcontainer/src/rootfs/mount.rs +++ b/crates/libcontainer/src/rootfs/mount.rs @@ -1,7 +1,7 @@ use std::fs::Permissions; use std::io::{BufRead, BufReader, ErrorKind}; use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd}; -use std::os::unix::fs::{MetadataExt, PermissionsExt}; +use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; use std::time::Duration; #[cfg(feature = "v1")] @@ -17,7 +17,7 @@ use nix::errno::Errno; use nix::fcntl::{AtFlags, OFlag, open, openat, readlinkat}; use nix::mount::MsFlags; use nix::sys::stat::{FchmodatFlags, Mode, SFlag, fchmodat, fstat, fstatat, mkdirat, mknodat}; -use nix::sys::statfs::{PROC_SUPER_MAGIC, statfs}; +use nix::sys::statfs::{PROC_SUPER_MAGIC, fstatfs}; use nix::unistd::{Gid, Uid, fchownat, symlinkat}; use oci_spec::runtime::{Mount as SpecMount, MountBuilder as SpecMountBuilder}; use pathrs::Root; @@ -211,8 +211,6 @@ impl Mount { _ => {} } - self.check_proc_mount(options.root, &dest_path, mount)?; - self.mount_into_container(mount, options.root, &mount_option_config, options.label) .map_err(|err| { tracing::error!("failed to mount {:?}: {}", mount, err); @@ -658,15 +656,20 @@ impl Mount { "mountpoint {real_dest:?} is on the top of rootfs {real_rootfs:?}" ))); } - self.check_proc_mount(&real_rootfs, &real_dest, m) - .inspect_err(|err| { - tracing::error!( - ?err, - destination = ?container_dest, - resolved = ?real_dest, - "proc-safety check failed" - ); - })?; + self.check_proc_mount( + &real_rootfs, + &real_dest, + m, + src_fd.as_ref().map(|fd| fd.as_fd()), + ) + .inspect_err(|err| { + tracing::error!( + ?err, + destination = ?container_dest, + resolved = ?real_dest, + "proc-safety check failed" + ); + })?; // fd-based mount flow: // - bind: open_tree -> mount_setattr -> move_mount @@ -882,7 +885,7 @@ impl Mount { m: &SpecMount, source: &Path, root: &Root, - dest_fd: BorrowedFd, + dest: BorrowedFd, mount_option_config: &MountOptionConfig, data_options: &[String], ) -> Result<()> { @@ -893,7 +896,7 @@ impl Mount { .collect(); self.syscall.mount( Some(source), - Path::new(&format!("/proc/self/fd/{}", dest_fd.as_raw_fd())), + Path::new(&format!("/proc/self/fd/{}", dest.as_raw_fd())), m.typ().as_deref(), mount_option_config.flags, Some(&data.join(",")), @@ -1010,6 +1013,8 @@ impl Mount { /// check_proc_mount checks to ensure that the mount destination is not over the top of /proc. /// dest_path is the host path of the mount destination under rootfs. It should have any /// symlinks resolved, otherwise a symlink in the rootfs can point the mount at /proc. + /// src is the opened source of a bind mount. A bind mount on /proc is allowed only if + /// src is on procfs. /// # Example (a valid case where `/proc` is mounted with `proc` type.) /// /// ``` @@ -1032,7 +1037,7 @@ impl Mount { /// .expect("failed to build SpecMount"); /// /// let dest_path = rootfs.join("proc"); - /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount).is_ok()); + /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount, None).is_ok()); /// ``` /// # Example (bind mount to `/proc` that should fail) /// ``` @@ -1055,13 +1060,14 @@ impl Mount { /// .expect("failed to build SpecMount"); /// /// let dest_path = rootfs.join("proc"); - /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount).is_err()); + /// assert!(mounter.check_proc_mount(&rootfs, &dest_path, &mount, None).is_err()); /// ``` pub fn check_proc_mount( &self, rootfs: &Path, dest_path: &Path, mount: &SpecMount, + src: Option, ) -> Result<()> { const PROC_ROOT_INO: u64 = 1; const VALID_PROC_MOUNTS: &[&str] = &[ @@ -1096,18 +1102,18 @@ impl Mount { } if is_bind(mount) { - if let Some(source) = mount.source() { - let stat = statfs(source).map_err(MountError::from)?; + if let Some(src) = src { + let stat = fstatfs(src).map_err(MountError::from)?; if stat.filesystem_type() == PROC_SUPER_MAGIC { - let meta = fs::metadata(source).map_err(MountError::from)?; + let ino = fstat(src).map_err(MountError::from)?.st_ino; // Follow the behavior of runc's checkProcMount function. - if meta.ino() != PROC_ROOT_INO { + if ino != PROC_ROOT_INO { tracing::warn!( "bind-mount {} (source {:?}) is of type procfs but not the root (inode {}). \ Future versions may reject this.", dest.display(), mount.source(), - meta.ino() + ino ); } return Ok(()); @@ -1217,7 +1223,7 @@ mod tests { #[cfg(feature = "v1")] use std::fs; use std::fs::OpenOptions; - use std::os::unix::fs::symlink; + use std::os::unix::fs::{MetadataExt, symlink}; use std::os::unix::net::UnixListener; use std::str::FromStr; @@ -1823,12 +1829,40 @@ mod tests { let dest_path = rootfs.path().join("proc"); assert!( mounter - .check_proc_mount(rootfs.path(), &dest_path, &mount) + .check_proc_mount(rootfs.path(), &dest_path, &mount, None) .is_ok() ); Ok(()) } + #[test] + fn test_check_proc_mount_bind_uses_source_fd() -> Result<()> { + let rootfs = tempfile::tempdir()?; + let mounter = Mount::new(); + + let mount = SpecMountBuilder::default() + .destination(PathBuf::from("/proc")) + .typ("bind".to_string()) + .source(PathBuf::from("/proc")) + .options(vec!["bind".to_string()]) + .build()?; + let src_fd = open( + rootfs.path(), + OFlag::O_PATH | OFlag::O_CLOEXEC, + Mode::empty(), + )?; + + let dest_path = rootfs.path().join("proc"); + let err = mounter + .check_proc_mount(rootfs.path(), &dest_path, &mount, Some(src_fd.as_fd())) + .unwrap_err(); + assert!( + err.to_string().contains("is not type proc"), + "unexpected error: {err}" + ); + Ok(()) + } + #[test] fn test_check_proc_mount_allowed_subpath() -> Result<()> { let rootfs = tempfile::tempdir()?; @@ -1844,7 +1878,7 @@ mod tests { assert!( mounter - .check_proc_mount(rootfs.path(), &uptime, &mount) + .check_proc_mount(rootfs.path(), &uptime, &mount, None) .is_ok() ); Ok(()) @@ -1865,7 +1899,7 @@ mod tests { assert!( mounter - .check_proc_mount(rootfs.path(), &custom, &mount) + .check_proc_mount(rootfs.path(), &custom, &mount, None) .is_err() ); Ok(())