From 13097b7d350da6e7721dccd8ef99f57a7788be46 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Thu, 25 May 2023 09:24:33 +0300 Subject: [PATCH] add rsymfollow recursive mount test Signed-off-by: Adrian Pop --- .../src/tests/mounts_recursive/mod.rs | 40 +++++++++++++++++++ .../runtimetest/src/tests.rs | 7 ++++ .../runtimetest/src/utils.rs | 22 ++++++++++ 3 files changed, 69 insertions(+) diff --git a/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs b/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs index 8609bf1f2e..107ecab874 100644 --- a/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs +++ b/tests/rust-integration-tests/integration_test/src/tests/mounts_recursive/mod.rs @@ -565,6 +565,44 @@ fn check_recursive_rnosymfollow() -> TestResult { result } +fn check_recursive_rsymfollow() -> TestResult { + let rsymfollow_dir_path = PathBuf::from_str("/tmp/rsymfollow").unwrap(); + let mount_dest_path = PathBuf::from_str("/mnt/rsymfollow").unwrap(); + fs::create_dir_all(rsymfollow_dir_path.clone()).unwrap(); + + let mount_options = vec![ + "rbind".to_string(), + "rsymfollow".to_string(), + "rsuid".to_string(), + ]; + let mut mount_spec = Mount::default(); + mount_spec + .set_destination(mount_dest_path) + .set_typ(None) + .set_source(Some(rsymfollow_dir_path.clone())) + .set_options(Some(mount_options)); + let spec = get_spec( + vec![mount_spec], + vec!["runtimetest".to_string(), "mounts_recursive".to_string()], + ); + let result = test_inside_container(spec, &|_| { + let original_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "file"); + let file = File::create(&original_file_path)?; + let link_file_path = format!("{}/{}", rsymfollow_dir_path.to_str().unwrap(), "link"); + let mut permission = file.metadata()?.permissions(); + permission.set_mode(permission.mode() | libc::S_ISUID | libc::S_ISGID); + file.set_permissions(permission) + .with_context(|| "failed to set permission")?; + + symlink(original_file_path, link_file_path)?; + println!("symlink success"); + Ok(()) + }); + + fs::remove_dir_all(rsymfollow_dir_path).unwrap(); + result +} + /// this mount test how to work? /// 1. Create mount_options based on the mount properties of the test /// 2. Create OCI.Spec content, container one process is runtimetest,(runtimetest is cargo model, file path `tests/rust-integration-tests/runtimetest/`) @@ -586,6 +624,7 @@ pub fn get_mounts_recursive_test() -> TestGroup { let rnoatime_test = Test::new("rnoatime_test", Box::new(check_recursive_rnoatime)); let rstrictatime_test = Test::new("rstrictatime_test", Box::new(check_recursive_rstrictatime)); let rnosymfollow_test = Test::new("rnosymfollow_test", Box::new(check_recursive_rnosymfollow)); + let rsymfollow_test = Test::new("rsymfollow_test", Box::new(check_recursive_rsymfollow)); let mut tg = TestGroup::new("mounts_recursive"); tg.add(vec![ @@ -604,6 +643,7 @@ pub fn get_mounts_recursive_test() -> TestGroup { Box::new(rnoatime_test), Box::new(rstrictatime_test), Box::new(rnosymfollow_test), + Box::new(rsymfollow_test), ]); tg diff --git a/tests/rust-integration-tests/runtimetest/src/tests.rs b/tests/rust-integration-tests/runtimetest/src/tests.rs index 9fe5256d2b..6245993995 100644 --- a/tests/rust-integration-tests/runtimetest/src/tests.rs +++ b/tests/rust-integration-tests/runtimetest/src/tests.rs @@ -240,6 +240,13 @@ pub fn validate_mounts_recursive(spec: &Spec) { eprintln!("path expected to be rnosymfollow, found not rnosymfollow, error: {e}"); } } + "rsymfollow" => { + if let Err(e) = utils::test_mount_rsymfollow_option( + mount.destination().to_str().unwrap(), + ) { + eprintln!("path expected to be rsymfollow, found not rsymfollow, error: {e}"); + } + } "rsuid" => { if let Err(e) = utils::test_mount_rsuid_option( mount.destination().to_str().unwrap(), diff --git a/tests/rust-integration-tests/runtimetest/src/utils.rs b/tests/rust-integration-tests/runtimetest/src/utils.rs index 863b57b337..a843c9b559 100644 --- a/tests/rust-integration-tests/runtimetest/src/utils.rs +++ b/tests/rust-integration-tests/runtimetest/src/utils.rs @@ -426,6 +426,28 @@ pub fn test_mount_rnosymfollow_option(path: &str) -> Result<(), std::io::Error> } } +pub fn test_mount_rsymfollow_option(path: &str) -> Result<(), std::io::Error> { + let path = format!("{}/{}", path, "link"); + let metadata = match symlink_metadata(path.clone()) { + Ok(metadata) => metadata, + Err(e) => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("get file symlink_metadata err {path:?}, {e}"), + )); + } + }; + // check symbolic is followed + if metadata.file_type().is_symlink() && metadata.mode() & 0o777 == 0o777 { + Ok(()) + } else { + Err(std::io::Error::new( + std::io::ErrorKind::Other, + format!("get file symlink_metadata err {path:?}"), + )) + } +} + pub fn test_mount_rsuid_option(path: &str) -> Result<(), std::io::Error> { let path = PathBuf::from(path).join("file");