wip Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
diff --git a/src/flags.rs b/src/flags.rs index 7377249..fd5692f 100644 --- a/src/flags.rs +++ b/src/flags.rs
@@ -303,5 +303,6 @@ pub struct ResolverFlags: u64 { // TODO: We should probably have our own bits... const NO_SYMLINKS = libc::RESOLVE_NO_SYMLINKS; + const NO_XDEV = libc::RESOLVE_NO_XDEV; } }
diff --git a/src/resolvers/opath/imp.rs b/src/resolvers/opath/imp.rs index fa437c6..0fdb929 100644 --- a/src/resolvers/opath/imp.rs +++ b/src/resolvers/opath/imp.rs
@@ -52,7 +52,7 @@ use crate::{ error::{Error, ErrorExt, ErrorImpl}, flags::{OpenFlags, ResolverFlags}, - procfs::ProcfsHandle, + procfs::{self, ProcfsHandle}, resolvers::{opath::SymlinkStack, PartialLookup, MAX_SYMLINK_TRAVERSALS}, syscalls, utils::{self, FdExt, PathIterExt}, @@ -245,6 +245,16 @@ ); let mut current = Rc::clone(&root); + // If the user asked for RESOLVE_NO_XDEV, fetch the mount id of the root + // once so we can compare against each component we walk into. Any mismatch + // means we crossed a mount boundary, and we emulate openat2(2) by + // returning EXDEV. + let no_xdev_root_mnt_id = if flags.contains(ResolverFlags::NO_XDEV) { + Some(utils::fetch_mnt_id(procfs.as_raw_procfs(), &*root, "")?) + } else { + None + }; + // Get initial set of components from the passed path. We remove components // as we do the path walk, and update them with the contents of any symlinks // we encounter. Path walking terminates when there are no components left. @@ -355,6 +365,24 @@ .wrap("check next '..' component didn't escape")?; } + // If the user asked for RESOLVE_NO_XDEV, verify we haven't + // crossed a mount boundary by walking into this component. + // Bind-mounts get their own mount id, so this catches bind + // mounts as well as "traditional" mounts on top of a dir. + // + // We propagate the error directly (matching openat2(2)) + // rather than returning a PartialLookup::Partial, because + // EXDEV counts as a safety violation and partial lookups + // short-circuit on such errors (see resolve_partial in + // ../openat2.rs). + if let Some(root_mnt_id) = no_xdev_root_mnt_id { + procfs::verify_same_mnt(procfs.as_raw_procfs(), root_mnt_id, &next, "") + .with_wrap(|| { + format!("check lookup of {part:?} didn't cross mount boundary") + }) + .wrap("emulated RESOLVE_NO_XDEV")?; + } + // Is the next dirfd a symlink or an ordinary path? If we're an // ordinary dirent, we just update current and move on to the // next component. Nothing special here.
diff --git a/src/syscalls.rs b/src/syscalls.rs index 3d1a86a..bc1cd45 100644 --- a/src/syscalls.rs +++ b/src/syscalls.rs
@@ -675,6 +675,8 @@ }) } +pub(crate) mod + pub(crate) mod openat2 { use super::*;
diff --git a/src/tests/common/root.rs b/src/tests/common/root.rs index c7c7047..7b4abcd 100644 --- a/src/tests/common/root.rs +++ b/src/tests/common/root.rs
@@ -374,6 +374,56 @@ Ok(()) } +pub(crate) fn create_overmount_tree() -> Result<TempDir, Error> { + Ok(create_tree! { + // A small tree that lives on the root mount. + "a" => (dir); + "a/file" => (file); + "b/c/d/e/f" => (dir); + "b/c/file" => (file); + // Placeholder directories for mounts to be applied on top of. + "mnt-bind" => (dir); // bind-mount of /a + "mnt-tmpfs" => (dir); // fresh tmpfs + "b/c/mnt-deep" => (dir); // deep bind-mount of /a + // Symlinks that traverse a mount boundary when followed. + "link-bind-abs" => (symlink -> "/mnt-bind"); + "link-bind-rel" => (symlink -> "mnt-bind"); + "link-tmpfs-abs" => (symlink -> "/mnt-tmpfs"); + "link-deep-abs" => (symlink -> "/b/c/mnt-deep"); + // Symlinks that don't cross mounts. + "link-a-abs" => (symlink -> "/a"); + "link-a-rel" => (symlink -> "a"); + }) +} + +pub(crate) fn apply_overmounts(root: &Path) -> Result<(), Error> { + let root_prefix = root.to_path_buf(); + + // Bind-mount /a over /mnt-bind. + tests_common::mount( + root_prefix.join("mnt-bind"), + MountType::Bind { + src: root_prefix.join("a"), + }, + )?; + // Tmpfs over /mnt-tmpfs. Populate with a file and subdir after mounting so + // we can test partial walks across the mount boundary. + tests_common::mount(root_prefix.join("mnt-tmpfs"), MountType::Tmpfs)?; + fs::File::create(root_prefix.join("mnt-tmpfs/file")) + .with_context(|| "create file in tmpfs mount")?; + fs::create_dir(root_prefix.join("mnt-tmpfs/subdir")) + .with_context(|| "mkdir in tmpfs mount")?; + // Bind-mount /a over a deeper path. + tests_common::mount( + root_prefix.join("b/c/mnt-deep"), + MountType::Bind { + src: root_prefix.join("a"), + }, + )?; + + Ok(()) +} + pub(crate) fn create_race_tree() -> Result<(TempDir, PathBuf), Error> { let tmpdir = create_tree! { // Our root.
diff --git a/src/tests/test_resolve.rs b/src/tests/test_resolve.rs index b00b4f9..32e370e 100644 --- a/src/tests/test_resolve.rs +++ b/src/tests/test_resolve.rs
@@ -469,6 +469,12 @@ symlink_component_nosym1: resolve("e/f", rflags = NO_SYMLINKS) => Err(ErrorKind::OsError(Some(libc::ELOOP))); symlink_component_nosym2: resolve("link2/link1_abs/target_rel", rflags = NO_SYMLINKS) => Err(ErrorKind::OsError(Some(libc::ELOOP))); loop_nosym: resolve("loop/link", rflags = NO_SYMLINKS) => Err(ErrorKind::OsError(Some(libc::ELOOP))); + // RESOLVE_NO_XDEV on a tree with no mounts should behave identically + // to a lookup with no resolver flags set. + dir_noxdev: resolve("b/c/d/e", rflags = NO_XDEV) => Ok(("b/c/d/e", libc::S_IFDIR)); + file_noxdev: resolve("b/c/file", rflags = NO_XDEV) => Ok(("b/c/file", libc::S_IFREG)); + symlink_noxdev: resolve("link3/target_abs", rflags = NO_XDEV) => Ok(("target", libc::S_IFDIR)); + dotdot_noxdev: resolve("b/c/d/e/f/../..", rflags = NO_XDEV) => Ok(("b/c/d", libc::S_IFDIR)); // fs.protected_symlinks for a directory owned by us. protected_symlinks_selfdir_selfsym: resolve("tmpfs-self/link-self") => Ok(("tmpfs-self/file", libc::S_IFREG)); protected_symlinks_selfdir_selfsym_nofollow: resolve("tmpfs-self/link-self", no_follow_trailing = true) => Ok(("tmpfs-self/link-self", libc::S_IFLNK)); @@ -513,6 +519,80 @@ } resolve_tests! { + [with_overmount_root] { + // Sanity-check: without NO_XDEV the mount setup still resolves fine. + #[cfg(feature = "_test_as_root")] + xdev_bind_baseline: resolve("mnt-bind") => Ok(("/mnt-bind", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_bind_subfile_baseline: resolve("mnt-bind/file") => Ok(("/mnt-bind/file", libc::S_IFREG)); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_baseline: resolve("mnt-tmpfs") => Ok(("/mnt-tmpfs", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_subfile_baseline: resolve("mnt-tmpfs/file") => Ok(("/mnt-tmpfs/file", libc::S_IFREG)); + #[cfg(feature = "_test_as_root")] + xdev_deep_baseline: resolve("b/c/mnt-deep") => Ok(("/b/c/mnt-deep", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_link_bind_baseline: resolve("link-bind-abs") => Ok(("/mnt-bind", libc::S_IFDIR)); + + // Paths that stay within the root mount succeed even with NO_XDEV. + #[cfg(feature = "_test_as_root")] + xdev_same_mount_dir: resolve("a", rflags = NO_XDEV) => Ok(("/a", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_same_mount_subfile: resolve("a/file", rflags = NO_XDEV) => Ok(("/a/file", libc::S_IFREG)); + #[cfg(feature = "_test_as_root")] + xdev_same_mount_deep: resolve("b/c/d/e/f", rflags = NO_XDEV) => Ok(("/b/c/d/e/f", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_same_mount_file: resolve("b/c/file", rflags = NO_XDEV) => Ok(("/b/c/file", libc::S_IFREG)); + // ".." lookups that stay inside the root mount are fine. + #[cfg(feature = "_test_as_root")] + xdev_same_mount_dotdot: resolve("a/..", rflags = NO_XDEV) => Ok(("/", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_same_mount_dotdot_deep: resolve("b/c/d/e/f/../..", rflags = NO_XDEV) => Ok(("/b/c/d", libc::S_IFDIR)); + // Symlinks that don't cross mount boundaries are fine. + #[cfg(feature = "_test_as_root")] + xdev_link_a_abs: resolve("link-a-abs", rflags = NO_XDEV) => Ok(("/a", libc::S_IFDIR)); + #[cfg(feature = "_test_as_root")] + xdev_link_a_rel: resolve("link-a-rel", rflags = NO_XDEV) => Ok(("/a", libc::S_IFDIR)); + + // Direct walks across a bind mount boundary fail with EXDEV. + #[cfg(feature = "_test_as_root")] + xdev_bind_direct: resolve("mnt-bind", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_bind_subfile: resolve("mnt-bind/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + // Direct walks across a tmpfs mount boundary fail with EXDEV. + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_direct: resolve("mnt-tmpfs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_subfile: resolve("mnt-tmpfs/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_subdir: resolve("mnt-tmpfs/subdir", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + // A mount crossing part-way through a deeper walk also fails. + #[cfg(feature = "_test_as_root")] + xdev_deep_direct: resolve("b/c/mnt-deep", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_deep_subfile: resolve("b/c/mnt-deep/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + + // Symlinks that lead across a mount boundary fail with EXDEV. + #[cfg(feature = "_test_as_root")] + xdev_link_bind_abs: resolve("link-bind-abs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_link_bind_rel: resolve("link-bind-rel", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_link_tmpfs_abs: resolve("link-tmpfs-abs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_link_deep_abs: resolve("link-deep-abs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + + // Combining NO_XDEV and NO_SYMLINKS: a symlink encountered before a + // mount boundary is reported as ELOOP (NO_SYMLINKS fires first), while + // a non-symlink mount crossing is still EXDEV. + #[cfg(feature = "_test_as_root")] + xdev_nosym_link_bind: resolve("link-bind-abs", rflags = NO_XDEV|NO_SYMLINKS) => Err(ErrorKind::OsError(Some(libc::ELOOP))); + #[cfg(feature = "_test_as_root")] + xdev_nosym_bind_direct: resolve("mnt-bind", rflags = NO_XDEV|NO_SYMLINKS) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + } +} + +resolve_tests! { [with_nosymfollow_root] { #[cfg(feature = "_test_as_root")] symlink_regular_symfollow: resolve("nosymfollow/goodlink") => Ok((".", libc::S_IFDIR)); @@ -579,6 +659,22 @@ }) } + pub(super) fn with_overmount_root<F>(func: F) -> Result<(), Error> + where + F: FnOnce(&Path) -> Result<(), Error>, + { + tests_common::in_mnt_ns(|| { + let root_dir = tests_common::create_overmount_tree()?; + + tests_common::apply_overmounts(root_dir.path())?; + + let res = func(root_dir.path()); + + let _root_dir = root_dir; // make sure tmpdir is kept alive + res + }) + } + pub(super) fn check_root_resolve<R, H>( root: R, unsafe_path: impl AsRef<Path>,
diff --git a/src/tests/test_resolve_partial.rs b/src/tests/test_resolve_partial.rs index 65e07ed..948434a 100644 --- a/src/tests/test_resolve_partial.rs +++ b/src/tests/test_resolve_partial.rs
@@ -696,6 +696,44 @@ } resolve_tests! { + [with_overmount_root] { + // Baseline (no NO_XDEV): mounts are traversed and lookups complete. + #[cfg(feature = "_test_as_root")] + xdev_bind_baseline: resolve_partial("mnt-bind/file") => Ok(PartialLookup::Complete(("/mnt-bind/file", libc::S_IFREG))); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_baseline: resolve_partial("mnt-tmpfs/file") => Ok(PartialLookup::Complete(("/mnt-tmpfs/file", libc::S_IFREG))); + + // In-mount paths succeed with NO_XDEV. + #[cfg(feature = "_test_as_root")] + xdev_same_mount_dir: resolve_partial("a", rflags = NO_XDEV) => Ok(PartialLookup::Complete(("/a", libc::S_IFDIR))); + #[cfg(feature = "_test_as_root")] + xdev_same_mount_deep: resolve_partial("b/c/d/e/f", rflags = NO_XDEV) => Ok(PartialLookup::Complete(("/b/c/d/e/f", libc::S_IFDIR))); + + // Crossing a mount boundary is a safety violation and is surfaced as + // an EXDEV error rather than a partial lookup, matching openat2. + #[cfg(feature = "_test_as_root")] + xdev_bind_direct: resolve_partial("mnt-bind", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_bind_subfile: resolve_partial("mnt-bind/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_direct: resolve_partial("mnt-tmpfs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_subfile: resolve_partial("mnt-tmpfs/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_deep_subfile: resolve_partial("b/c/mnt-deep/file", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + // Missing path inside a mount is still an EXDEV (the crossing fails first). + #[cfg(feature = "_test_as_root")] + xdev_tmpfs_missing: resolve_partial("mnt-tmpfs/does-not-exist", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + + // Symlinks that resolve across a mount boundary also fail with EXDEV. + #[cfg(feature = "_test_as_root")] + xdev_link_bind_abs: resolve_partial("link-bind-abs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + #[cfg(feature = "_test_as_root")] + xdev_link_tmpfs_abs: resolve_partial("link-tmpfs-abs", rflags = NO_XDEV) => Err(ErrorKind::OsError(Some(libc::EXDEV))); + } +} + +resolve_tests! { [with_nosymfollow_root] { #[cfg(feature = "_test_as_root")] symlink_regular_symfollow: resolve_partial("nosymfollow/goodlink") => Ok(PartialLookup::Complete((".", libc::S_IFDIR))); @@ -766,6 +804,23 @@ }) } + pub(super) fn with_overmount_root<F>(func: F) -> Result<(), Error> + where + F: FnOnce(Root) -> Result<(), Error>, + { + tests_common::in_mnt_ns(|| { + let root_dir = tests_common::create_overmount_tree()?; + let root = Root::open(&root_dir)?; + + tests_common::apply_overmounts(root_dir.path())?; + + let res = func(root); + + let _root_dir = root_dir; // make sure tmpdir is kept alive + res + }) + } + pub(super) fn check_root_resolve_partial( root: &Root, unsafe_path: impl AsRef<Path>,
diff --git a/src/utils/fd.rs b/src/utils/fd.rs index 2891f29..20805a3 100644 --- a/src/utils/fd.rs +++ b/src/utils/fd.rs
@@ -502,6 +502,7 @@ } .into() }) + .or_else(|_: Error| -> Result<Option<_>, Error> {})? .or_else(|_: Error| -> Result<Option<_>, Error> { // openat doesn't support O_EMPTYPATH, so if we are operating on "" we // should reuse the dirfd directly.