oxedyne/fe2o3/fe2o3_core/tests/secret_chain.rs
4.0 KiB, 1 run
created by r1870400018:74716, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #![cfg(unix)] |
| 2 | |
| 3 | use oxedyne_fe2o3_core::prelude::*; |
| 4 | use oxedyne_fe2o3_core::file::{ |
| 5 | effective_uid, |
| 6 | secure_secret_chain, |
| 7 | secure_secret_file, |
| 8 | }; |
| 9 | |
| 10 | use std::{ |
| 11 | fs, |
| 12 | os::unix::fs::{ |
| 13 | MetadataExt, |
| 14 | PermissionsExt, |
| 15 | symlink, |
| 16 | }, |
| 17 | path::{ |
| 18 | Path, |
| 19 | PathBuf, |
| 20 | }, |
| 21 | time::{ |
| 22 | SystemTime, |
| 23 | UNIX_EPOCH, |
| 24 | }, |
| 25 | }; |
| 26 | |
| 27 | |
| 28 | /// A directory that removes itself however the test ends. |
| 29 | struct Scratch { |
| 30 | path: PathBuf, // where it is |
| 31 | } |
| 32 | |
| 33 | impl Scratch { |
| 34 | fn new(what: &str) -> Outcome<Self> { |
| 35 | let stamp = res!(SystemTime::now().duration_since(UNIX_EPOCH)); |
| 36 | let path = std::env::temp_dir().join(fmt!( |
| 37 | "fe2o3_chain_{}_{}_{}", what, std::process::id(), stamp.as_nanos())); |
| 38 | res!(fs::create_dir_all(&path)); |
| 39 | Ok(Self { path }) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Drop for Scratch { |
| 44 | fn drop(&mut self) { |
| 45 | let _ = fs::remove_dir_all(&self.path); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn mode(path: &Path) -> Outcome<u32> { |
| 50 | Ok(res!(fs::metadata(path)).permissions().mode() & 0o777) |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn test_effective_uid_owns_what_this_process_creates() -> Outcome<()> { |
| 55 | let s = res!(Scratch::new("uid")); |
| 56 | let file = s.path.join("mine"); |
| 57 | res!(fs::write(&file, b"x")); |
| 58 | assert_eq!(res!(effective_uid()), res!(fs::metadata(&file)).uid()); |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_secure_secret_chain_narrows_each_existing_component() -> Outcome<()> { |
| 64 | let s = res!(Scratch::new("narrow")); |
| 65 | let a = s.path.join("a"); |
| 66 | res!(fs::create_dir_all(a.join("b"))); |
| 67 | res!(fs::set_permissions(&a, fs::Permissions::from_mode(0o755))); |
| 68 | res!(fs::set_permissions(a.join("b"), fs::Permissions::from_mode(0o777))); |
| 69 | // The last component is not there yet, which ends the walk. |
| 70 | let narrowed = res!(secure_secret_chain(&s.path, Path::new("a/b/c/d"))); |
| 71 | assert_eq!(narrowed, vec![(a.clone(), 0o755), (a.join("b"), 0o777)], "each is reported with its old mode"); |
| 72 | assert!(res!(secure_secret_chain(&s.path, Path::new("a/b/c/d"))).is_empty(), "and only once"); |
| 73 | assert_eq!(res!(mode(&a)), 0o700); |
| 74 | assert_eq!(res!(mode(&a.join("b"))), 0o700); |
| 75 | assert!(!a.join("b").join("c").exists()); |
| 76 | Ok(()) |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn test_secure_secret_chain_refuses_a_symbolic_link() -> Outcome<()> { |
| 81 | let s = res!(Scratch::new("link")); |
| 82 | let open = s.path.join("open"); |
| 83 | res!(fs::create_dir_all(&open)); |
| 84 | res!(fs::set_permissions(&open, fs::Permissions::from_mode(0o777))); |
| 85 | res!(fs::create_dir_all(s.path.join("a"))); |
| 86 | let link = s.path.join("a").join("b"); |
| 87 | res!(symlink(&open, &link)); |
| 88 | let msg = match secure_secret_chain(&s.path, Path::new("a/b/c")) { |
| 89 | Ok(_) => return Err(err!("A link in the chain was not refused."; Test, Missing)), |
| 90 | Err(e) => fmt!("{}", e), |
| 91 | }; |
| 92 | assert!(msg.contains(&fmt!("{:?}", link)), "{}", msg); |
| 93 | assert!(msg.contains("symbolic link"), "{}", msg); |
| 94 | assert_eq!(res!(mode(&open)), 0o777, "nothing beyond the link was touched"); |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn test_secure_secret_chain_refuses_another_users_directory() -> Outcome<()> { |
| 100 | // `/tmp` is root's; as root there is nobody else to be, so nothing is asked. |
| 101 | if res!(effective_uid()) == 0 { |
| 102 | return Ok(()); |
| 103 | } |
| 104 | let msg = match secure_secret_chain(Path::new("/"), Path::new("tmp/x")) { |
| 105 | Ok(_) => return Err(err!("Root's directory was not refused."; Test, Missing)), |
| 106 | Err(e) => fmt!("{}", e), |
| 107 | }; |
| 108 | assert!(msg.contains("\"/tmp\"") && msg.contains("owned by user 0"), "{}", msg); |
| 109 | Ok(()) |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn test_secure_secret_file_refuses_a_link_and_passes_a_missing_file() -> Outcome<()> { |
| 114 | let s = res!(Scratch::new("file")); |
| 115 | res!(secure_secret_file(&s.path.join("absent"))); |
| 116 | let real = s.path.join("real"); |
| 117 | res!(fs::write(&real, b"k")); |
| 118 | res!(secure_secret_file(&real)); |
| 119 | let link = s.path.join("key"); |
| 120 | res!(symlink(&real, &link)); |
| 121 | assert!(secure_secret_file(&link).is_err()); |
| 122 | if res!(effective_uid()) != 0 { |
| 123 | assert!(secure_secret_file(Path::new("/etc/passwd")).is_err()); |
| 124 | } |
| 125 | Ok(()) |
| 126 | } |